【问题标题】:QTabWidget‘s setCornerWidget not working if using customized widget如果使用自定义小部件,QTabWidget 的 setCornerWidget 不起作用
【发布时间】:2021-06-16 08:27:20
【问题描述】:

我的自定义角小部件包含一些 qlabels 和 qpushbuttons,我想将其设置为 qtabwidget 的右上角,如下所示:

但是当我运行这个应用程序时,角落小部件没有出现。我仅使用 QLabel 作为角落小部件进行了测试,它可以工作。 那么为什么qtabwidget的setCornerWidget不能使用自定义的widget,有没有办法解决呢?

代码sn-ps:

ma​​inwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QLabel>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    // NOT work
    customBar = new CustomWidget();
    ui->tabWidget->setCornerWidget(customBar);

    //Work
    QLabel* label = new QLabel("test text");
    ui->tabWidget->setCornerWidget(label);
}

MainWindow::~MainWindow()
{
    delete ui;
}

customwidget.cpp

#include "customwidget.h"
#include "ui_customwidget.h"

CustomWidget::CustomWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::CustomWidget)
{
    ui->setupUi(this);
}

CustomWidget::~CustomWidget()
{
    delete ui;
}

ma​​inwindow.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>400</width>
    <height>300</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralWidget">
   <widget class="QTabWidget" name="tabWidget">
    <property name="geometry">
     <rect>
      <x>10</x>
      <y>10</y>
      <width>381</width>
      <height>231</height>
     </rect>
    </property>
    <property name="currentIndex">
     <number>1</number>
    </property>
    <property name="tabsClosable">
     <bool>false</bool>
    </property>
    <property name="movable">
     <bool>true</bool>
    </property>
    <widget class="QWidget" name="tab">
     <attribute name="title">
      <string>tab1</string>
     </attribute>
    </widget>
    <widget class="QWidget" name="tab_2">
     <attribute name="title">
      <string>tab2</string>
     </attribute>
    </widget>
   </widget>
  </widget>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <resources/>
 <connections/>
</ui>

customwidget.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>CustomWidget</class>
 <widget class="QWidget" name="CustomWidget">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>131</width>
    <height>26</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Form</string>
  </property>
  <widget class="QLabel" name="label">
   <property name="geometry">
    <rect>
     <x>10</x>
     <y>0</y>
     <width>81</width>
     <height>21</height>
    </rect>
   </property>
   <property name="text">
    <string>custom widget</string>
   </property>
  </widget>
  <widget class="QPushButton" name="pushButton">
   <property name="geometry">
    <rect>
     <x>100</x>
     <y>0</y>
     <width>25</width>
     <height>23</height>
    </rect>
   </property>
   <property name="text">
    <string/>
   </property>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>

【问题讨论】:

  • 分享 .ui 文件
  • @eyllanesc 更新了它

标签: qt qtabwidget


【解决方案1】:

我在另一个 StackOverflow 地方找到了类似的 question。它还在 QTabWidget 的右上角显示多个组件。给出的方法 Gediminas是用代码一步一步实现的:

        QWidget* pTabCornerWidget = new QWidget(this);

        QLabel* pLabelTime = new QLabel(pTabCornerWidget);
        pLabelTime->setText("10:22:20");

        QPushButton* pButton = new QPushButton(pTabCornerWidget);
        pButton->setText("?");
        pButton->setMaximumSize(QSize(25, 25));

        QHBoxLayout* pHLayout = new QHBoxLayout(pTabCornerWidget);
        pHLayout->addWidget(pLabelTime);
        pHLayout->addWidget(pButton);

        ui->tabWidget->setCornerWidget(pTabCornerWidget, Qt::TopRightCorner);

我在我的电脑上试了一下,确实没问题,但实际上我的自定义角小部件有 11 个组件和它们自己的样式表:

如果用代码来绘制的话,这会比在设计界面中设计ui要难一些,而且要占用很多行代码。我以为我必须将我的自定义小部件类型明确指定为 QWidget*,但它仍然不起作用:

    //Still not work
    CustomWidget* customBar = new CustomWidget();
    qDebug()<<customBar;
    QWidget* w = qobject_cast<QWidget*>(customBar);
    qDebug()<<w;
    ui->tabWidget->setCornerWidget(w,Qt::TopRightCorner);

【讨论】:

    猜你喜欢
    • 2020-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-23
    • 1970-01-01
    • 2018-05-25
    相关资源
    最近更新 更多