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. Embed a QWizard inside QTabWidget

Embed a QWizard inside QTabWidget

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 3 Posters 1.9k Views 3 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.
  • J Offline
    J Offline
    Jendker
    wrote on last edited by Jendker
    #1

    I am having problem with QTabWidget and QWizard. I am trying to put the QWizard, created with Qt Designer inside one of the tabs with the following code, but it does not show my element.

    I am trying something like this:

    auto layout = new QVBoxLayout;
    ui->sampling->setLayout(layout);
    
    // add example image
    auto example_image = new QLabel(ui->sampling);
    example_image->setObjectName(QStringLiteral("example_image"));
    example_image->setPixmap(QPixmap(QString::fromUtf8(":/res/Resources/pexels-photo-248797.jpeg")));
    example_image->setScaledContents(true);
    
    // add wizard
    auto sampling_wizard = new SamplingWizard(ui->sampling);
    
    // add to layout
    layout->addWidget(example_image);
    layout->addWidget(sampling_wizard);
    

    Where ui->sampling is the tab object.

    But the result is only:
    Tab image

    (showing just the image, not wizard in the tab)

    I've tried also simple

    auto sampling_wizard = new SamplingWizard(ui->sampling);
    

    but it does not show anything. Juggling a bit with the sizePolicy parameters for QWizard in Qt Designer also did not help.

    Still, the example found here: QTBUG case
    In my case:

    auto sampling_wizard = new SamplingWizard(ui->sampling);
    setCentralWidget(sampling_wizard);
    

    works fine and my (very simple) QWizard is shown correctly:
    Image

    What am I missing? I was already struggling for a couple of hours to achieve this

    Thank you!

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

      Hi,

      Note I haven't test it but from the looks of it, you are not using QTabWidget correctly. You should rather build a QWidget that will contain the vertical layout and add that to your QTabWidget. If you already have all the tabs from your Designer design then you should set the layout on the widget contained in the tab you want to show your wizard on.

      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
      2
      • J Jendker

        I am having problem with QTabWidget and QWizard. I am trying to put the QWizard, created with Qt Designer inside one of the tabs with the following code, but it does not show my element.

        I am trying something like this:

        auto layout = new QVBoxLayout;
        ui->sampling->setLayout(layout);
        
        // add example image
        auto example_image = new QLabel(ui->sampling);
        example_image->setObjectName(QStringLiteral("example_image"));
        example_image->setPixmap(QPixmap(QString::fromUtf8(":/res/Resources/pexels-photo-248797.jpeg")));
        example_image->setScaledContents(true);
        
        // add wizard
        auto sampling_wizard = new SamplingWizard(ui->sampling);
        
        // add to layout
        layout->addWidget(example_image);
        layout->addWidget(sampling_wizard);
        

        Where ui->sampling is the tab object.

        But the result is only:
        Tab image

        (showing just the image, not wizard in the tab)

        I've tried also simple

        auto sampling_wizard = new SamplingWizard(ui->sampling);
        

        but it does not show anything. Juggling a bit with the sizePolicy parameters for QWizard in Qt Designer also did not help.

        Still, the example found here: QTBUG case
        In my case:

        auto sampling_wizard = new SamplingWizard(ui->sampling);
        setCentralWidget(sampling_wizard);
        

        works fine and my (very simple) QWizard is shown correctly:
        Image

        What am I missing? I was already struggling for a couple of hours to achieve this

        Thank you!

        A Offline
        A Offline
        ambershark
        wrote on last edited by
        #3

        @Jendker So I was curious and decided to test this out... it works fine for me. Here is my example project with it working so you can look through your code and see what you did wrong.

        Screenshot of it working:
        0_1520739810293_twtest.png

        MainWindow.cpp:

        #include <QtWidgets/QVBoxLayout>
        #include <QtWidgets/QLabel>
        #include "MainWindow.h"
        #include "MyWizard.h"
        
        MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
        {
        	resize(600, 400);
        
        	auto *tw = new QTabWidget();
        	auto *w = new QWidget();
        	auto *layout = new QVBoxLayout();
        	w->setLayout(layout);
        
        	layout->addWidget(new QLabel("Some Widget"));
        	layout->addWidget(new MyWizard());
        
        	tw->addTab(w, "Test");
        	setCentralWidget(tw);
        }
        

        MyWizard is just a random wizard in a .ui. If you need the rest of the project let me know and I will put it up here. However my guess is your problem is in the part I pasted above.

        My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

        J 1 Reply Last reply
        3
        • A ambershark

          @Jendker So I was curious and decided to test this out... it works fine for me. Here is my example project with it working so you can look through your code and see what you did wrong.

          Screenshot of it working:
          0_1520739810293_twtest.png

          MainWindow.cpp:

          #include <QtWidgets/QVBoxLayout>
          #include <QtWidgets/QLabel>
          #include "MainWindow.h"
          #include "MyWizard.h"
          
          MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
          {
          	resize(600, 400);
          
          	auto *tw = new QTabWidget();
          	auto *w = new QWidget();
          	auto *layout = new QVBoxLayout();
          	w->setLayout(layout);
          
          	layout->addWidget(new QLabel("Some Widget"));
          	layout->addWidget(new MyWizard());
          
          	tw->addTab(w, "Test");
          	setCentralWidget(tw);
          }
          

          MyWizard is just a random wizard in a .ui. If you need the rest of the project let me know and I will put it up here. However my guess is your problem is in the part I pasted above.

          J Offline
          J Offline
          Jendker
          wrote on last edited by Jendker
          #4

          @ambershark
          Thank you, I solved my problem! The thing was that I was assigning the tab (QWidget) as parent and it somehow prevents adding the QWizard (SamplingWizard) to layout. What is interesting, if I am assigning the tab as the parent in QLabel constructor it does not have any negative effects (and it should not, I guess).

          If I will have some time I will check how it behaves on Linux and MacOS and report the bug if necessary, till now I was checking this only under Windows. Maybe it is also somehow platform dependant.

          @SGaist
          I guess you were describing the steps which @ambershark included in his code? Maybe I did not emphasize enough that QTabWidget was created with Qt Designer, so the QTabWidget had to be created correctly, I've also checked the code generated by Qt Designer and it does exactly this what was in the included code. Nevertheless thank you for your remark!

          If I can ask in the same question what I am not sure about, is it possible I assign the layout to tab (QWidget) in Qt Designer? All the layout options in the content menu are greyed out and if I first select the QWidget in the right-hand toolbar and select any option from the menu on top it does not work as intended (changes only layout of the centralWidget of MainWindow). This would be quite beneficial as Qt Designer gives nice overview of the changes made and hides the code very neatly.

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

            There's no code involved with Designer. You get an XML with the description of your widget and its content (layout, other widgets etc.) that is then put in place when ui->setup(this) is called. As for QMainWindow, you can't put a layout on it because it already has one that manages all the fancy stuff a QMainWindow should do.

            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
            1
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Unless you where thinking of the code generated by the uic tool ?

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

              J 1 Reply Last reply
              0
              • SGaistS SGaist

                Unless you where thinking of the code generated by the uic tool ?

                J Offline
                J Offline
                Jendker
                wrote on last edited by
                #7

                @SGaist That would be it :) I should have said code generated by the uic tool. Thanks for clearing this out.

                1 Reply Last reply
                0
                • J Jendker

                  @ambershark
                  Thank you, I solved my problem! The thing was that I was assigning the tab (QWidget) as parent and it somehow prevents adding the QWizard (SamplingWizard) to layout. What is interesting, if I am assigning the tab as the parent in QLabel constructor it does not have any negative effects (and it should not, I guess).

                  If I will have some time I will check how it behaves on Linux and MacOS and report the bug if necessary, till now I was checking this only under Windows. Maybe it is also somehow platform dependant.

                  @SGaist
                  I guess you were describing the steps which @ambershark included in his code? Maybe I did not emphasize enough that QTabWidget was created with Qt Designer, so the QTabWidget had to be created correctly, I've also checked the code generated by Qt Designer and it does exactly this what was in the included code. Nevertheless thank you for your remark!

                  If I can ask in the same question what I am not sure about, is it possible I assign the layout to tab (QWidget) in Qt Designer? All the layout options in the content menu are greyed out and if I first select the QWidget in the right-hand toolbar and select any option from the menu on top it does not work as intended (changes only layout of the centralWidget of MainWindow). This would be quite beneficial as Qt Designer gives nice overview of the changes made and hides the code very neatly.

                  A Offline
                  A Offline
                  ambershark
                  wrote on last edited by
                  #8

                  @Jendker said in Embed a QWizard inside QTabWidget:

                  The thing was that I was assigning the tab (QWidget) as parent and it somehow prevents adding the QWizard

                  Yea this can be a problem. Sometimes Qt isn't great at reparenting things. I tend to not assign parents to objects I know are going to be reparented when I add them to layouts in other widgets.

                  I was actually going to mention that but I didn't have enough background code to see what you were doing to know if I was correct or not. That is why I decided to just write some example code which I figured would help you figure it out. :)

                  If I will have some time I will check how it behaves on Linux and MacOS and report the bug if necessary, till now I was checking this only under Windows. Maybe it is also somehow platform dependant.

                  Pretty unlikely here. I wouldn't waste your time. I write my code cross platform for win, mac, and linux. I've never had an issue with parenting that existed only on 1 platform. I'm not saying it can't happen but my guess is if I ran your broken code on linux or mac it would be broken just like in windows.

                  From the sounds of things it isn't even a bug but instead a coding error where you set a parent you shouldn't have.

                  My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

                  J 1 Reply Last reply
                  2
                  • A ambershark

                    @Jendker said in Embed a QWizard inside QTabWidget:

                    The thing was that I was assigning the tab (QWidget) as parent and it somehow prevents adding the QWizard

                    Yea this can be a problem. Sometimes Qt isn't great at reparenting things. I tend to not assign parents to objects I know are going to be reparented when I add them to layouts in other widgets.

                    I was actually going to mention that but I didn't have enough background code to see what you were doing to know if I was correct or not. That is why I decided to just write some example code which I figured would help you figure it out. :)

                    If I will have some time I will check how it behaves on Linux and MacOS and report the bug if necessary, till now I was checking this only under Windows. Maybe it is also somehow platform dependant.

                    Pretty unlikely here. I wouldn't waste your time. I write my code cross platform for win, mac, and linux. I've never had an issue with parenting that existed only on 1 platform. I'm not saying it can't happen but my guess is if I ran your broken code on linux or mac it would be broken just like in windows.

                    From the sounds of things it isn't even a bug but instead a coding error where you set a parent you shouldn't have.

                    J Offline
                    J Offline
                    Jendker
                    wrote on last edited by Jendker
                    #9

                    @ambershark Thank you for your reply, it clarifies a lot things. I will be more careful with the proper setup of parent.

                    1 Reply Last reply
                    1

                    • Login

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