Results 1 to 9 of 9

Thread: Sharing same QToolbar amongst multiple QMainWindows

  1. #1
    Join Date
    Dec 2013
    Location
    Colorado
    Posts
    45
    Thanks
    15
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Sharing same QToolbar amongst multiple QMainWindows

    I'm trying to use the same toolbar with a parent QMainWindow and its children QMainWindows.
    Same actions, same stylesheet, same icons, etc.
    The following likely violates all sense of Qt decency, but it kind of works, but only for the first few instantiations of the child QMainWindows.
    I know I'm just using pointers, but is there a better way other than painstakingly building a new toolbar for each child QMainWindow?


    Qt Code:
    1. SubWin::SubWin(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::SubWin)
    4. {
    5. ui->setupUi(this);
    6. setAttribute(Qt::WA_DeleteOnClose);
    7.  
    8. foreach ( QWidget* widget, qApp->allWidgets() )
    9. {
    10. if ( QToolBar* mainTB = qobject_cast<QToolBar*>( widget ) )
    11. {
    12. if( mainTB->objectName() == "mainToolBar" )
    13. {
    14. QToolBar *NewTB = new QToolBar("New Tool Bar", 0);
    15.  
    16. this->addToolBar(Qt::TopToolBarArea, NewTB );
    17. NewTB->setMovable(false);
    18.  
    19. NewTB->setStyleSheet(mainTB->styleSheet());
    20.  
    21. NewTB->addActions(mainTB->actions());
    22. }
    23. break;
    24. }
    25. }
    26.  
    27. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Henry Blue Heeler; 28th December 2013 at 06:54. Reason: grammar

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    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: Sharing same QToolbar amongst multiple QMainWindows

    There is no reason to share toolbars between main windows. It is the actions that should be shared, the toolbars should remain separate.
    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
    Dec 2013
    Location
    Colorado
    Posts
    45
    Thanks
    15
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Sharing same QToolbar amongst multiple QMainWindows

    I'm not really sharing toolbars, I'm adding a new toolbar for every new window. Sharing actions? Do I create a list of actions then addActions()?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    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: Sharing same QToolbar amongst multiple QMainWindows

    Quote Originally Posted by Henry Blue Heeler View Post
    I'm not really sharing toolbars, I'm adding a new toolbar for every new window.
    Yes, and that's a proper approach. Of course not considering HOW you are creating those toolbars.
    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.


  5. #5
    Join Date
    Dec 2013
    Location
    Colorado
    Posts
    45
    Thanks
    15
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Sharing same QToolbar amongst multiple QMainWindows

    Well, how should I create the toolbars? Please criticize the example code above.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    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: Sharing same QToolbar amongst multiple QMainWindows

    Do not copy contents of one toolbar to the other. Have a function that will create identical toolbars instead.

    Qt Code:
    1. QToolBar *createToolBar(QMainWindow *window) {
    2. QToolBar *tb = new QToolBar(window);
    3. tb->addAction(action1);
    4. tb->addAction(action2);
    5. // ...
    6. return tb;
    7. }
    To copy to clipboard, switch view to plain text mode 
    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.


  7. The following user says thank you to wysota for this useful post:

    Henry Blue Heeler (29th December 2013)

  8. #7
    Join Date
    Dec 2013
    Location
    Colorado
    Posts
    45
    Thanks
    15
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Sharing same QToolbar amongst multiple QMainWindows

    A function for creating common toolbars is exactly what I've been working on. You confirmed my suspicions.
    The code example at the top was trying to copy/clone the toolbar et al from the parent QMainWindow to the children.
    I know, bad form.
    I can't second guess why no QToolBar copy constructor is available, that would certainly make this whole exercise moot.

    Thanks for the help.

  9. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    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: Sharing same QToolbar amongst multiple QMainWindows

    Quote Originally Posted by Henry Blue Heeler View Post
    I can't second guess why no QToolBar copy constructor is available, that would certainly make this whole exercise moot.
    For exactly the same reason why any QObject derived class cannot be copied -- QObject instances are identities, not values. For instance, if you make a number of signal/slot connections to the toolbar, should a copy have those connections as well or not?
    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.


  10. #9
    Join Date
    Dec 2013
    Location
    Colorado
    Posts
    45
    Thanks
    15
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Sharing same QToolbar amongst multiple QMainWindows

    I want the same slots for all the toolbars and now it's working great.
    Thanks again for the help!
    Something like
    Qt Code:
    1. void MainWindow::CreateToolBar( QMainWindow *parent )
    2. {
    3. QToolBar *tb = parent->addToolBar("test");
    4. QAction *actionPrint = tb->addAction( QObject::tr("Print Record"), this, SLOT(on_actionPrint_triggered()));
    5. actionPrint->setIcon(QPixmap(":/Images/printer.bmp"));
    6. actionPrint->setText(QObject::tr("Print Record"));
    7. actionPrint->setToolTip(QObject::tr("Print Record"));
    8. // ...
    9. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. [QtDesigner] Multiple rows of QToolBar
    By Malutek in forum Qt Programming
    Replies: 2
    Last Post: 19th January 2013, 15:23
  2. how to set the QToolBar's actions to multiple rows
    By fly542 in forum Qt Programming
    Replies: 2
    Last Post: 25th July 2011, 03:00
  3. Replies: 4
    Last Post: 2nd July 2010, 21:16
  4. Replies: 4
    Last Post: 17th October 2009, 22:31
  5. How to change between QMainWindows???
    By webquinty in forum Newbie
    Replies: 3
    Last Post: 16th October 2009, 10:46

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.