QT常用UI设计技巧

1.QTab的tab选项按钮的位置的调节

可以通过修改tabPosition的东西南北来实现

2.如何在关闭主窗口的同时来关闭子窗口

//主窗口
this->setAttribute(Qt::WA_QuitOnClose,true);

//所有子窗口
this->setAttribute(Qt::WA_QuitOnClose,false);

参考链接:1.父窗口关闭后子窗口仍然存在

3.多屏时控制子窗口显示在当前屏幕位置

QDesktopWidget *deskWgt = QApplication::desktop();
int curMonitor = deskWgt->screenNumber(this);
QRect screen = deskWgt->screenGeometry(curMonitor);

myUI.move(screen.x() + (screen.width() - myUI.width())/2, screen.y()+(screen.height() - myUI.height())/2);

参考链接:1.qt多显示器获取不同显示器的分辨率和位置

                  2.Qt 解决窗口在多个显示器上显示位置设置问题

4.QComoBox设置指定选项不可选并改变背景颜色

//disable the comobox item which is 111 or 222
QComboBox* myBox = new QComboBox();
myBox->addItem(tr("000"));
myBox->addItem(tr("111"));
myBox->addItem(tr("222"));
myBox->addItem(tr("333"));
myBox->addItem(tr("444"));

QVariant v(0);
myBox->setItemData(1, v, Qt::UserRole - 1);
myBox->setItemData(1, QColor(192,192,192), Qt::BackgroundColorRole);
myBox->setItemData(2, v, Qt::UserRole - 1);
myBox->setItemData(2, QColor(192,192,192), Qt::BackgroundColorRole);

myBox->setCurrentIndex(0);

5.QComoBox字体显示居中

    QComboBox* box = new QComboBox(this);
    QLineEdit* lineEdit = new QLineEdit(this);

    QHBoxLayout* layoutBox = new QHBoxLayout;
    layoutBox->setSpacing(0);
    layoutBox->setMargin(0);
    layoutBox->addWidget(lineEdit);
    box->setLayout(layoutBox);
    //box->setLineEdit(m_lineeidt); 不要使用这个,否则只有箭头区域可以选中
    connect(box, SIGNAL(currentIndexChanged(const QString&)), lineEdit, SLOT(setText(const QString&)));

    lineEdit->setReadOnly(true);
    lineEdit->setEnabled(false);
    lineEdit->setAlignment(Qt::AlignCenter);

    QStringList itemss;
    itemss << "offline" << "student" << "teacher" << "manager";
    QListWidget *listWidget = new QListWidget(this);
    for(int i = 0;i < itemss.count();++i)
    {
        QListWidgetItem *item = new QListWidgetItem(itemss.at(i));
        item->setTextAlignment(Qt::AlignCenter);
        listWidget->addItem(item);
    }

    box->setModel(listWidget->model());
    box->setView(listWidget);

    box->setStyleSheet("QComboBox {"
                                "color:black;"
                                "background-color:white;"
                                "outline:0px;"
                                "border:0px;"
                                "}"
                                "QComboBox::down-arrow{"
                                "right:3px;"
                                "image:url('subscriptLogin.png');"
                                "}"
                                "QComboBox:drop-down {"
                                "outline:0px;"
                                "border:0px;"
                                " }");

    auto layout = new QHBoxLayout();
    this->setLayout(layout);
    layout->addWidget(box);

参考连接:1.QCombobox内容居中(包含展示内容,下拉列表)

                  2.QComboBox中文字和下拉项居中

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值