Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] fill parent of widget with tabwidget
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] fill parent of widget with tabwidget

Scheduled Pinned Locked Moved General and Desktop
4 Posts 1 Posters 6.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ? This user is from outside of this forum
    ? This user is from outside of this forum
    Guest
    wrote on last edited by
    #1

    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:
    http://codeprogress.com/cpp/libraries/qt/qtChangeTabTextColor.php#.VCa0S44vC-8
    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].setText(st[i]);
        l[i].setAlignment(Qt::AlignCenter);     // works only within Layout
        //l[i].setStyleSheet("QLabel { color: blue; font-size: 22px; }");
        vbl[i].addWidget(&l[i]);
        //w[i].setStyleSheet("QWidget { background-color: green; }");
        w[i].setLayout(&vbl[i]);
        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; }");
    
    mw->setCentralWidget(wc);
    mw->show();
    
    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]

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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);
      layout->addWidget(label);

      tabWidget->addTab(tabName, widget);
      }

      tmMainWindow mw;
      mw.setCentralWidget(tabWidget);
      mw.show();
      return app.exec();
      @

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • ? This user is from outside of this forum
        ? This user is from outside of this forum
        Guest
        wrote on last edited by
        #3

        Hi!

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

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

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

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved