Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved How to center a QToolBar at QMainWindow?

    General and Desktop
    2
    4
    2265
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • X
      xtingray last edited by

      Hi,
      I have been looking for a solution for this question in several forums and I have found several approaches but I didn't like any of them.
      Using this line I can set my toolbar in the bottom of my app:

      addToolBar(Qt::BottomToolBarArea, myToolbar); 
      

      But it is always left-aligned, is there a way to set it in the middle of that bottom panel? Also, Is there a way to make it look similar to the dock of the OSX desktop?

      Thanks!


      Qt Developer

      1 Reply Last reply Reply Quote 0
      • Chris Kawa
        Chris Kawa Moderators last edited by Chris Kawa

        To keep it simple you can insert two expanding dummies at the start and end of the toolbar:

        auto dummy1 = new QWidget(this);
        dummy1->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
        auto dummy2 = new QWidget(this);
        dummy2->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
        
        auto tb = new QToolBar();
        tb->addWidget(dummy1);
        tb->addAction("foo");
        tb->addAction("bar");
        tb->addAction("bazz");
        tb->addWidget(dummy2);
        
        addToolBar(Qt::BottomToolBarArea, tb);
        
        1 Reply Last reply Reply Quote 2
        • X
          xtingray last edited by

          Thanks! It worked perfect for me :)

          PS: I still miss the day you could do something like:

          addToolBar(Qt::BottomToolBarArea|Qt::CenterToolBarArea , myToolbar); 
          

          Qt Developer

          1 Reply Last reply Reply Quote 0
          • Chris Kawa
            Chris Kawa Moderators last edited by Chris Kawa

            Toolbar area in main window and buttons alignment within a toolbar are two entirely separate concepts so it wouldn't make sense to put these two options together.
            Would be nice however if there was something like QToolBar::setAlignment(Qt::Alignment), although toolbars can be placed on the sides too, so directions become a little fuzzy.

            1 Reply Last reply Reply Quote 0
            • First post
              Last post