QT如何在widget类中设置居中显示?

yingge2016 2014-10-11 09:23:29
QT如何在widget类中设置居中显示?
...全文
8125 12 打赏 收藏 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
jiangxinhuan 2014-12-29
  • 打赏
  • 举报
回复 2
QWidget *widget = new QWidget(); widget.setAlignment(Qt::AlignCenter或者Qt::ALignHCenter) 这样,在调用widget时就会居中显示了。 另外,在layout中添加widget时,也可以对该widget进行居中显示设置。 vLayout.addWidget(widget,1,Qt::Aligncenter)
yangchuankai 2014-10-13
  • 打赏
  • 举报
回复
引用 8 楼 xiaofeng6552538 的回复:
[quote=引用 7 楼 u012199908 的回复:] [quote=引用 5 楼 xytx_823 的回复:] 可以重写QWidget,在构造函数中根据父类设置自己的位置
引用 6 楼 u010002704 的回复:
继承实现吧,,,自己爱咋样咋样,, 头像好像是个女的,本人?? 我得先去念一下经
是不是像楼上说的那样,在构造函数中写 myWidget::myWidget(QWidget *parent) : QWidget(parent) { this->setStyleSheet("background-color:red"); this->move((parent->width()-this->width())/2, (parent->width()-this->width())/2); } 程序崩溃啊。[/quote] 程序崩溃??你把你的代码贴出来看看。[/quote] 大哥,她让QWidget子类在QWidget身上移动,而QWidget在哪,子类能移哪去?继承QWidget根本行不通阿,继承QWidgetItem还可能,,,小弟拙见,刚学Qt
yangchuankai 2014-10-13
  • 打赏
  • 举报
回复
是不是像楼上说的那样,在构造函数中写 myWidget::myWidget(QWidget *parent) : QWidget(parent) { this->setStyleSheet("background-color:red"); this->move((parent->width()-this->width())/2, (parent->width()-this->width())/2); } 程序崩溃啊。[/quote] 人家QWidget和QWidgetItem 不是一个东西好不好,,,我有个比较笨的方法: 获得item的总行列数,然后循环设置每一个item里的内容(文字或图片)居中,,(这个不难吧),
小吥点 2014-10-13
  • 打赏
  • 举报
回复
引用 7 楼 u012199908 的回复:
[quote=引用 5 楼 xytx_823 的回复:] 可以重写QWidget,在构造函数中根据父类设置自己的位置
引用 6 楼 u010002704 的回复:
继承实现吧,,,自己爱咋样咋样,, 头像好像是个女的,本人?? 我得先去念一下经
是不是像楼上说的那样,在构造函数中写 myWidget::myWidget(QWidget *parent) : QWidget(parent) { this->setStyleSheet("background-color:red"); this->move((parent->width()-this->width())/2, (parent->width()-this->width())/2); } 程序崩溃啊。[/quote] 程序崩溃??你把你的代码贴出来看看。
yingge2016 2014-10-13
  • 打赏
  • 举报
回复
引用 5 楼 xytx_823 的回复:
可以重写QWidget,在构造函数中根据父类设置自己的位置
引用 6 楼 u010002704 的回复:
继承实现吧,,,自己爱咋样咋样,, 头像好像是个女的,本人?? 我得先去念一下经
是不是像楼上说的那样,在构造函数中写 myWidget::myWidget(QWidget *parent) : QWidget(parent) { this->setStyleSheet("background-color:red"); this->move((parent->width()-this->width())/2, (parent->width()-this->width())/2); } 程序崩溃啊。
yangchuankai 2014-10-12
  • 打赏
  • 举报
回复
继承实现吧,,,自己爱咋样咋样,, 头像好像是个女的,本人?? 我得先去念一下经
yingge2016 2014-10-11
  • 打赏
  • 举报
回复
引用 3 楼 xiaofeng6552538 的回复:
[quote=引用 2 楼 u012199908 的回复:] [quote=引用 1 楼 xiaofeng6552538 的回复:] [quote=引用 楼主 u012199908 的回复:] QT如何在widget类中设置居中显示?[/quot 可以设置参数,一般居中的话用Qt::AlignCenter
widget 如何使用?[/quote] 你的意思是让widget居中显示?如果是widget的话 QWidget*widget=new QWidget; int wd=widget->width(); int ht=widget->height(); this->move((wd-width())/2,(ht-height())/2); [/quote] 我是想,在WIDGET类中设置居中显示的属性,别的地方调用他的时候,不用再设置,直接SHOW就能让他居中。
小吥点 2014-10-11
  • 打赏
  • 举报
回复
引用 2 楼 u012199908 的回复:
[quote=引用 1 楼 xiaofeng6552538 的回复:] [quote=引用 楼主 u012199908 的回复:] QT如何在widget类中设置居中显示?[/quot 可以设置参数,一般居中的话用Qt::AlignCenter
widget 如何使用?[/quote] 你的意思是让widget居中显示?如果是widget的话 QWidget*widget=new QWidget; int wd=widget->width(); int ht=widget->height(); this->move((wd-width())/2,(ht-height())/2);
yingge2016 2014-10-11
  • 打赏
  • 举报
回复
引用 1 楼 xiaofeng6552538 的回复:
[quote=引用 楼主 u012199908 的回复:] QT如何在widget类中设置居中显示?[/quot 可以设置参数,一般居中的话用Qt::AlignCenter
widget 如何使用?
小吥点 2014-10-11
  • 打赏
  • 举报
回复
[quote=引用 楼主 u012199908 的回复:] QT如何在widget类中设置居中显示?[/quot 可以设置参数,一般居中的话用Qt::AlignCenter
xytx_823 2014-10-11
  • 打赏
  • 举报
回复
可以重写QWidget,在构造函数中根据父类设置自己的位置

21,503

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧