1. 程式人生 > >QT tabwidget 程式碼新增標籤頁的方法(可以最大化)

QT tabwidget 程式碼新增標籤頁的方法(可以最大化)

QT用程式碼對介面進行佈局時,按常規思路在tabwidget遇到一個很大的問題:就是最大化後有問題,沒有實現最大化的效果。

後來通過各種查詢最後又找到一種方法對tabwidget新增頁:

//.h檔案
class Data : public QMainWindow
{
    Q_OBJECT

public:
    Data(QWidget *parent = Q_NULLPTR);
private:
    Ui::DataClass ui;
    QTabWidget  *m_TabWidget;

    void initTabWidget();
};

class BackDataTab : public
QWidget { Q_OBJECT public: explicit BackDataTab(QWidget *parent = 0); public: const int m_EditHight; QLabel *m_LabelFilterWidgetYMax, *m_LabelFilterWidgetYMin, *m_LabelFFTWidgetYMax; QLineEdit *m_LineEditFilterWidgetYMax, *
m_LineEditFilterWidgetYMin, *m_LineEditFFTWidgetYMax, *m_LineEditVideoPath; QCheckBox *m_CheckBoxPass1, *m_CheckBoxPass2, *m_CheckBoxPass3, *m_CheckBoxPass4; QPushButton *m_PushBtnVideo; QGroupBox *
m_GroupChioceShow, *m_GroupChoicePass, *m_GroupFilterAFFTYValue, *m_GroupVideo; QRadioButton *m_RadioBtnFilter, *m_RadioBtnNotch; QCustomPlot *m_CustomFilter, *m_CustomFFT; private slots: void slotBtnVideo(); void slotCheckPass1(); void slotCheckPass2(); void slotCheckPass3(); void slotCheckPass4(); private: QVector<int> m_VectCurPass; void ChoiceCheck(); void initSignalSlot(); }; //.cpp檔案 Data::Data(QWidget *parent) : QMainWindow(parent) { ui.setupUi(this); // 顯示部分佈局 m_TabWidget = new QTabWidget; m_TabWidget->addTab(new BackDataTab(), QString::fromLocal8Bit("資料回放")); setCentralWidget(m_TabWidget); setWindowState(Qt::WindowMaximized); } BackDataTab::BackDataTab(QWidget *parent /* = 0 */) :QWidget(parent) , m_EditHight(30) { m_GroupVideo = new QGroupBox(QString::fromLocal8Bit("選擇要回放的錄影")); m_LineEditVideoPath = new QLineEdit(); m_PushBtnVideo = new QPushButton(tr("...")); m_PushBtnVideo->setFixedWidth(m_EditHight); m_PushBtnVideo->setFixedHeight(m_EditHight); m_LineEditVideoPath->setFixedHeight(m_EditHight); m_LineEditVideoPath->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); QHBoxLayout *HBLayoutVideo = new QHBoxLayout(); HBLayoutVideo->addWidget(m_LineEditVideoPath); HBLayoutVideo->addWidget(m_PushBtnVideo); HBLayoutVideo->addSpacing(0); m_GroupVideo->setLayout(HBLayoutVideo); ... QHBoxLayout *HBLayoutTab = new QHBoxLayout(); HBLayoutTab->addLayout(VBLayoutChartView); HBLayoutTab->addLayout(VBLayoutTab); HBLayoutTab->setStretchFactor(VBLayoutChartView, 8); HBLayoutTab->setStretchFactor(VBLayoutTab, 2); HBLayoutTab->setSpacing(0); // 設定控制元件中元素間距 setLayout(HBLayoutTab); }

原來使用的另一種方式是:

QWidget *widgetBackData = new QWidget();
widgetBackData->setLayout(HBLayoutTab2);
m_TabWidget->addTab(widgetBackData, QString::fromLocal8Bit("資料回放"));
setCentralWidget(m_TabWidget);

這種的最大化的時候就會出現問題,問了一些大佬說,這種的是因為最頂層不是layout導致的,不是特別明白要怎麼做,有大佬懂得,歡迎指點。