Results 1 to 4 of 4

Thread: Manually adding a QWidget to a QToolbar in the .UI code -> element order messed up?

  1. #1
    Join Date
    Jan 2012
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Question Manually adding a QWidget to a QToolbar in the .UI code -> element order messed up?

    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?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Manually adding a QWidget to a QToolbar in the .UI code -> element order messed u

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jan 2012
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Manually adding a QWidget to a QToolbar in the .UI code -> element order messed u

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

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Manually adding a QWidget to a QToolbar in the .UI code -> element order messed u

    Quote Originally Posted by Brecht View Post
    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?

    Qt Code:
    1. class MyWidget: public QMainWindow {
    2. public:
    3. MyWidget(QWidget *parent = 0) : QMainWindow(parent) {
    4. ui->setupUi(this);
    5. ui->toolBar->insertWidget(new QPushButton("Hey"), ui->someAction);
    6. }
    7. ~MyWidget() { delete ui; }
    8. private:
    9. Ui::MyWidget *ui;
    10. };
    To copy to clipboard, switch view to plain text mode 

    No need to copy & paste anything anywhere.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 80
    Last Post: 12th March 2020, 14:58
  2. Replies: 0
    Last Post: 20th September 2010, 10:58
  3. Adding element to QTableWidget.
    By sudhansu in forum Qt Programming
    Replies: 0
    Last Post: 6th March 2010, 08:16
  4. Deleting a QWidget derived object manually
    By hubbobubbo in forum Qt Programming
    Replies: 2
    Last Post: 4th February 2010, 17:32
  5. Adding a QWidget to QToolbar
    By qtUser500 in forum Qt Tools
    Replies: 6
    Last Post: 16th October 2009, 19:16

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.