Qt World Summit: Register Today!

Crashing when changing currentIndex of stackedWidget...

  • So i have 5 buttons and a stacked widget with 5 pages.. button 1 changes the page to 1, button 2 page 2....
    the code to change the currentIdex:
    @ ui->stackedWidget->setCurrentIndex(page_here);

    Well after doing this:

    @ createOptionsGroupBox();
    mainLayout = new QVBoxLayout;
    QWidget *widget = new QWidget();

    void MainWindow::createOptionsGroupBox()
    optionsGroupBox = new QGroupBox();
    optionsGroupBoxLayout = new QGridLayout;
    optionsGroupBoxLayout->addWidget(ui->widget, 0, 0, 1, 9);
    optionsGroupBoxLayout->addWidget(ui->musicButton, 0,0);
    optionsGroupBoxLayout->addWidget(ui->sep1, 0, 1);
    optionsGroupBoxLayout->addWidget(ui->imageButton, 0,4);
    optionsGroupBoxLayout->addWidget(ui->rom_deviceButton, 0,6);
    optionsGroupBoxLayout->addWidget(ui->advancedButton, 0,8);
    optionsGroupBoxLayout->addWidget(ui->stackedWidget, 1, 0, 1, 9);

    it crashes when you click one of the 5 buttons so basically it crashes when passing from
    @ ui->stackedWidget->setCurrentIndex(page_here);

    Any ideas on what could cause that problem?

  • Have you confirmed by stepping through the code via debugger that the line you mention is actually the place where it crashes? What exactly is page_here and is it valid?

    PS: I still think you should learn to use QtCreator for layouting. You're making life alot harder than it needs to be. Sure, there are complicated parts of some UIs that need dynamic layouts that need to be coded by hand. But this very simple layout is absolutely a job for WYSIWYG layouting with QtCreator/Designer. Look at this video tutorial for an introduction to layouts with QtCreator: https://www.youtube.com/watch?v=2edb0VOkx-k
    That channel has further videos looking at other layout classes.

  • Yes i have confirmed via debugger that the
    @ ui->stackedWidget->setCurrentIndex(page_here);
    is the line that the programm crashes.. in my code i dont have page_here instead i have 0,1,2,3 which is valid pages of the stacked widget.. I will see the video that you said but as i know there are 2 solution to make your window resizable.. ui and by coding.. it's not acceptable from qt to crash in something like this.. what if i didn't want to make it by ui and was forced to do it via code? What would be the solution to such a problem?

  • bq. it’s not acceptable from qt to crash in something like this

    Correct. And it isn't Qt, it's a bug in your code. Possibly something that gets triggered when switching pages. Update events, focus events, paint events...
    You'll need to debug your program deeper. Look at stack traces, see which code portions are executed after or during page switches etc.

    Of course you can create layouts by hand coding it, without any crashes. You're just not using appropriate tools to make life easier. But if you want to do this by hand, I won't stop you. You can also write code in vim or emacs and compile and debug via console commands.

  • Well it's not that my mainwindow has 1 label 1 line edit and 2 buttons.. it's really more compicated.. For example i have set the window as a Grid. i have a widget with a background, 5 buttons (with some stylesheet ) and 4 labels( 1 width in order to separate the buttons.. ).. this is how when not maximized :


    and maximized:

    Ignore the fact that the rest of the window is in a mess.. see the toolbar.. u can see that only the widget resizes, the buttons and the separators stay there :/

  • You say it crashes in the line

    But there are three questions here.

    1. is "ui" valid?
    2. is "ui->stackedWidget" valid?
    3. is "page_here" valid?
      Now, you've answered 3), so next you should make sure your ui member is valid, and then that ui->stackedWidget is valid. Did you remember to call setupUi(this);?

  • everything is valid..
    the momment i comment these lines

    @ /*
    mainLayout = new QVBoxLayout;
    QWidget widget = new QWidget();


    @ ui->stackedWidget->setCurrentIndex(page_here);

    will not crash the program

  • Still don't see your problem with QtDesigner, really. I think you just didn't take the time to find out how it works. It'd pay off.
    Select the five top buttons and click the horizontal layout button. Select the six side buttons and click vertical layout. select the bottom button, label and combobox elements and click horizotal layout. Then select the table widget and the side-button-layout and click horizontal layout. Then select the main window and click vertical layout.
    Done, takes about 20 seconds.

    PS: What's the code of createButtonsLayout?

  • [quote author="DerManu" date="1347713268"]Still don't see your problem with QtDesigner, really. I think you just didn't take the time to find out how it works. It'd pay off.
    Select the five top buttons and click the horizontal layout button. Select the six side buttons and click vertical layout. select the bottom button, label and combobox elements and click horizotal layout. Then select the table widget and the side-button-layout and click horizontal layout. Then select the main window and click vertical layout.
    Done, takes about 20 seconds.

    PS: What's the code of createButtonsLayout?[/quote]

    My friend DerManu thank you a lot, i played about 1 hour and managed to do something good. But with 2 questions, if you have the time to help me... So this is what i managed:


    And the 2 questions..
    1st.. in the toolbar ( the 5 buttons ) as i have said they had some separators.. 1 width each one.. here you can see a screenshot that the separators dissapeared the moment a right clicked->layout->horizontally..


    2nd.. The groubox cannot work well with hidden objects.. for example u can see in the first screeenshot i attached here, the progressbar is not visible.. when i show it it appears normally

    but when u hide it ( sorry for bad quality in this one )

    you see it leaves an empty space, where that empty space shouldn't be there.. :/
    the same is with start/stop buttons.. it's really ugly. .

  • About the spacers: set an appropriate minimum width (1px).
    About the progress bar: Not sure what's going on there. I've tried it with a horizontal layout, and as soon as I set the visiblity of the progress bar to false, there is no space left but the other objects in the layout take up the space.

  • About the separators: seemed like a qt bug, i reloaded the pixmaps and everything is ok..
    About the progressbar: well see how it is in the designer:


    what do you suggest me about it?

    And one more.. as you can see in this screenshot :

    the start converting and output format and combox is not in the middle of the listwidget.. in order to do that i should had the listwidget and these 3 widgets in a vertical layout.. but this can't happen because the listwidget is already in a layout with the buttons ( add,addfolder etc )... any ideas?

    Thank you, really thank you for answering until now.. You really helped me understand some things :)

  • To center the button/label/combobox under the view, use a QGridLayout with bottom right cell empty. Top left is the view, top right the vertically aligned buttons and bottom left the horizontally aligned button/label/combobox.

    Please elaborate on the progressbar issue. I don't see what's wrong with it.

  • i am on another computer right now.. i will see what you said about the grid layout..

    for the progressbar look:

    On starting programm the windows looks like this

    the programm is a converter.. when you convert for example a video it shows a progressbar

    and when finished it hides the progressbar

    but you can see that an ugly space created there.. why is that happening.. you can see my designer here do you have any suggestion?

  • Is the topmost layout a QVBoxLayout (vertical layout) where the progressbar is one cell (the third from top, after the toolbuttons and the view-sidebuttons-layout)? If so, then it should actually work. I've created a similar situation for testing and there is no space left when hiding the progressbar.

  • I am just dump :)
    i wasn't hiding the progressbar but i was using QGraphicsOpacityEffect to set the opacity to 0 with a cool animation.. but when the opacity was 0 i was forgotting to call ->hide();

    i have solved any of my previous problems, but i want to question one last thing and i am ready, look..
    This is the window in the designer:

    And this is my mainwindows on action ( the arrows were added after the screenshot was taken ):

    Do you see that empty space? I want the show errors buttons hidden , and visible only when there was an error at conversion of 1 file ( as said the programm is a converter )..

    Well i can't hide the whole layout which inside there are a horizonal space and the show erros button because it says that u can't hide a horizontal spacer.. What the hek? What i am supposed to do for this?

  • It doesn't work when just hiding the show-errors-button? Normally horizontal spacers shouldn't have a significant minimal height and thus the whole row should collapse horizontally to a few pixels (e.g. top+bottom margin of the layout).

    Just a workaround idea: Why not place the button in the bottom right cell of the grid layout?

  • Not it doesn't work when just hiding the show-errors-button..
    and i cannot put it in the grid cause the grid is a stackedwidget.. i want the show erros buttons to be visible regardless the page of stacked widget..

    my workaround idea is to set the height of the spacer to 0...
    now it seems a little better :)



  • Xmm and how about adding a widget from code?
    I mean i have all this widgets layouted in my designer.. what about if i have created a widget in my code and i wanted to add to the window?

    yes there is a reason why i created it by code..

    from a quick think i thought of making an empty stackedwidget ( that is layouted properly in the designer ), and then via code say something like ui->stackedwidget->addwidget(blabla).. but there isn't an addwidget function.. any ideas?

  • Leon, look into widget promotion for that. Insert the closest base class of your custom widget into your layout, and promote it to the real widget using the right-click menu -> promote... Alternatively: create a Qt Designer plugin for your custom widget. The latter is probably only worth-while for widgets that you expect to use regulary from designer.

Log in to reply