PDA

View Full Version : Qt Designer Manually adding a QWidget to a QToolbar in the .UI code -> element order messed up?



Brecht
27th January 2012, 14:57
I know you can't add QWidget's to a QToolbar in the designer.

So I tried to copy-paste the UI code from the QWidget into the QToolbar's section.
I've found an old document about the .ui format and it says you can do this.
It does work, even though the widget is not shown in the designer.
The header file gets generated with the widget in the toolbar and it runs ok.

Only problem, the item order is not preserved. I want the widget to be placed on the right. But it always gets placed first, no matter where it's put in the UI xml...

Is there a way around this, or is this a bug/not-implemented-feature in the UI-compiler?

Has someone successfully done it, or should I just give up and write my toolbar's in code?

wysota
27th January 2012, 17:10
The question is why would you want to change the ui file this way instead of saving yourself a lot of effort and adding that widget in code like you are supposed to do?

If you're so keen on modifying files, you can work around your problem by generating the c++ class from your ui file and editing the resulting file by moving the section of code responsible for the widget later in setupUi() method so that it gets added to the toolbar later. Just be aware you will have to do that every time you modify your .ui file or call make clean.

Brecht
30th January 2012, 08:48
Thank you for the reply, and pointing me in the right direction.
I had been looking at the UI compiler source to do a quick hack, but that is way too much effort...

My solution now is to copy-paste the generated code into my own class, and using InsertWidget instead.
This way I can keep using the designer for the other parts of the window, just not that one frame on the toolbar...

wysota
30th January 2012, 09:44
My solution now is to copy-paste the generated code into my own class, and using InsertWidget instead.
This way I can keep using the designer for the other parts of the window, just not that one frame on the toolbar...
Why are you not doing it the proper way?


class MyWidget: public QMainWindow {
public:
MyWidget(QWidget *parent = 0) : QMainWindow(parent) {
ui->setupUi(this);
ui->toolBar->insertWidget(new QPushButton("Hey"), ui->someAction);
}
~MyWidget() { delete ui; }
private:
Ui::MyWidget *ui;
};

No need to copy & paste anything anywhere.