如果使用自定义小部件,QTabWidget 的 setCornerWidget 将不起作用

如何解决如果使用自定义小部件,QTabWidget 的 setCornerWidget 将不起作用

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

enter image description here

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

代码片段:

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>

解决方法

我在 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);

我在电脑上试了一下确实可以,但其实我自定义的corner widget有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);

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?