Qt World Summit: Register Today!

[SOLVED] fill parent of widget with tabwidget

  • Hi!

    I know this is a general task and I have seen several questions related this topic but I did not find the answer for my case.
    I would like to ask how can I fill the parent widget of my qtabwidget? anything I have tried di not help me.
    I saw this example code:
    but here the author used setFixedSize, but I would like to do with changeable size. (follow its parent)

    Here is my code:
    (tmMainWindow is just a class inherited from QMainWindow, nothing special)

    //#include <QCoreApplication>
    #include <QApplication>

    #include "tmmainwindow.h"
    #include <QWidget>
    #include <QtWidgets/QTabWidget>
    #include <QVBoxLayout>
    #include <QtWidgets/QLabel>

    int main(int argc, char *argv[])
    // texts for each tab
    QString st[] = {"file & directory", "date & time", "thread & lock",
    "network & socket", "serial"};
    int size = sizeof(st) / sizeof(st[0]); // size of text array

    //QCoreApplication a(argc, argv);
    QApplication a(argc, argv);
    tmMainWindow* mw = new tmMainWindow(&a);
    QWidget*      wc = new QWidget;             // center widget for tab widget
    QTabWidget*   tw = new QTabWidget(wc);      // tab widget for tabs
    QWidget*       w = new QWidget[size];
    QLabel*        l = new QLabel[size];
    QVBoxLayout* vbl = new QVBoxLayout[size];
    // each tab -> label, layout, widget, tab
    for(int i = 0; i < size; ++i)
        l[i].setAlignment(Qt::AlignCenter);     // works only within Layout
        //l[i].setStyleSheet("QLabel { color: blue; font-size: 22px; }");
        //w[i].setStyleSheet("QWidget { background-color: green; }");
        tw->addTab(&w[i], l[i].text());
    // tab widget
    //wt->setStyleSheet("QWidget { background-color: yellow; }");
    //tw->setStyleSheet("QTabWidget { background-color: black; font-size: 18px; }");
    // main window
    mw->resize(800, 600);
    mw->setWindowTitle("tab and menu");
    //mw->setStyleSheet("tmMainWindow { background-color: dark grey; }");
    return a.exec&#40;&#41;;


    I am glad if someone has a great idea or solution for me. (I would like to learn and know whether my theory is on the wrong way or not)

    Have a nice day :)

    [edit: added missing coding tags @ SGaist]

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    It's too much complication

    QApplication a(argc, argv); // <- first thing to do

    QStringList tabNames;
    tabNames << "file & directory"
    << "date & time"
    << "thread & lock"
    << "network & socket"
    << "serial";
    QTabWidget *tabWidget = new QTabWidget
    foreach(const QString& tabName, tabNames) {
    QWidget *widget = new QWidget;
    QVBoxLayout *layout = new QVBoxLayout(widget);
    QLabel *label = new QLabel(tabName);

    tabWidget->addTab(tabName, widget);

    tmMainWindow mw;
    return app.exec();

  • Hi!

    It works as cool as it can :)
    Thanks a lot, you teached me some basic theories for Qt developing.

  • Lifetime Qt Champion

    You're welcome ! :)

    Since it's all good now, please update the thread tittle prepending [solved] so other forum users may know a solution has been found :)

Log in to reply