0

My customized corner widget cantains some qlabels and qpushbuttons, i want to set it to the top right corner of qtabwidget that looks like this : enter image description here

But when i run this application, the corner widget didn't show up. I tested using simply a QLabel as corner widget, it works. So why using customized widget does not work for qtabwidget's setCornerWidget, and is there a way to solve it ?

The code snippets:

mainwindow.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;
}

mainwindow.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>

Wade Wang
  • 536
  • 6
  • 11

1 Answers1

0

I found a similar question in another StackOverflow place. It also displays multiple components in the top-right corner of QTabWidget. The method given by Gediminas is to implement it step by step with code:

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

I tried it on my computer and it was indeed ok, but actually my custom corner widget has 11 components and their own style sheets: enter image description here

If using code to draw it, this will become a little harder and take up many lines of code than designing the ui in the design interface. I thought that i had to explicitly specify my custom widgettype as QWidget*, but it still doesn't work:

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