Embed a QWizard inside QTabWidget



  • 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!


  • Lifetime Qt Champion

    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.


  • Moderators

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



  • @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.


  • Lifetime Qt Champion

    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.


  • Lifetime Qt Champion

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



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


  • Moderators

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



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.