Crashing when changing currentIndex of stackedWidget...
-
wrote on 14 Sept 2012, 12:10 last edited by
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;
mainLayout->addWidget(optionsGroupBox);
QWidget *widget = new QWidget();
widget->setLayout(mainLayout);
setCentralWidget(widget);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->videoButton,0,2);
optionsGroupBoxLayout->addWidget(ui->sep2,0,3);
optionsGroupBoxLayout->addWidget(ui->imageButton, 0,4);
optionsGroupBoxLayout->addWidget(ui->sep3,0,5);
optionsGroupBoxLayout->addWidget(ui->rom_deviceButton, 0,6);
optionsGroupBoxLayout->addWidget(ui->sep4,0,7);
optionsGroupBoxLayout->addWidget(ui->advancedButton, 0,8);
optionsGroupBoxLayout->addWidget(ui->stackedWidget, 1, 0, 1, 9);
optionsGroupBox->setLayout(optionsGroupBoxLayout);
}@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?
-
wrote on 15 Sept 2012, 10:02 last edited by
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. -
wrote on 15 Sept 2012, 10:07 last edited by
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? -
wrote on 15 Sept 2012, 10:36 last edited by
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.
-
wrote on 15 Sept 2012, 12:19 last edited by
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 :
!http://i.imgur.com/6maXD.png(1)!
and maximized:
!http://i.imgur.com/1f6EB.png(2)!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 :/
-
wrote on 15 Sept 2012, 12:30 last edited by
You say it crashes in the line
@ui->stackedWidget->setCurrentIndex(page_here);@But there are three questions here.
- is "ui" valid?
- is "ui->stackedWidget" valid?
- 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);?
-
wrote on 15 Sept 2012, 12:42 last edited by
everything is valid..
the momment i comment these lines@ /*
createOptionsGroupBox();
createButtonsLayout();
mainLayout = new QVBoxLayout;
mainLayout->addWidget(optionsGroupBox);
mainLayout->addLayout(buttonsLayout);
QWidget widget = new QWidget();
widget->setLayout(mainLayout);
setCentralWidget(widget);/@the
@ ui->stackedWidget->setCurrentIndex(page_here);
@will not crash the program
-
wrote on 15 Sept 2012, 12:47 last edited by
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?
-
wrote on 16 Sept 2012, 09:37 last edited by
[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:
!http://i.imgur.com/m1dnx.png(1)!
!http://i.imgur.com/HR2Jf.png(2)!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..!http://i.imgur.com/l3J2K.png(3)!
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
!http://i.imgur.com/uizKE.png(4)!but when u hide it ( sorry for bad quality in this one )
!http://i.imgur.com/j443P.png(5)!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. . -
wrote on 16 Sept 2012, 14:41 last edited by
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. -
wrote on 16 Sept 2012, 16:13 last edited by
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:!http://i.imgur.com/pgK1e.png(designer)!
what do you suggest me about it?
And one more.. as you can see in this screenshot :
!http://i.imgur.com/8pJRu.png(screen)!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 :)
-
wrote on 16 Sept 2012, 20:31 last edited by
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.
-
wrote on 16 Sept 2012, 21:12 last edited by
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
!http://i.imgur.com/m1dnx.png(1)!the programm is a converter.. when you convert for example a video it shows a progressbar
!http://i.imgur.com/uizKE.png(1)!and when finished it hides the progressbar
!http://i.imgur.com/j443P.png(3)!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?
!http://i.imgur.com/l3J2K.png(4)! -
wrote on 16 Sept 2012, 21:44 last edited by
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.
-
wrote on 17 Sept 2012, 14:42 last edited by
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:
!http://i.imgur.com/lw1kT.png(1)!And this is my mainwindows on action ( the arrows were added after the screenshot was taken ):
!http://i.imgur.com/aT50f.png(2)!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?
-
wrote on 17 Sept 2012, 21:38 last edited by
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?
-
wrote on 17 Sept 2012, 21:56 last edited by
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 :)height=20
!http://i.imgur.com/ckofX.png(20)!height=0
!http://i.imgur.com/LxW8z.png(0)! -
wrote on 18 Sept 2012, 09:54 last edited by
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?
-
wrote on 18 Sept 2012, 11:37 last edited by
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.
1/19