Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Cant set Height or SizePolicy for QToolbar

Cant set Height or SizePolicy for QToolbar

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 3.0k Views
  • 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.
  • Fuel 0F Offline
    Fuel 0F Offline
    Fuel 0
    wrote on last edited by
    #1

    i have added a QToolBar manually, because in Qt Designer i didnt found an Option to use Layouts in the QToolBar. I have 2 QToolbars in a vertical Layout at the left Side. One QToolBar has Top Alignment and the other Bottom. The Problem is that the Top Bar dont stretch to the bottom Bar. I have some empty Space. i tried to set QSizePolicy Expanding, Preferred and MinimumExpanding. I used setFixedHeight(), but nothing seems to change the Size of the Bar.

    0_1525228953604_Screenshot_20180502_044049.png

    This is my actually Code. I nearly tried everything

    QString stylesheet = "QToolBar { background-color: blue; }";
    
        QToolBar *barTop = new QToolBar(this);
        barTop->setMovable(false);
        barTop->setFixedWidth(60);
        barTop->setFixedHeight(200);
        barTop->setOrientation(Qt::Vertical);
        barTop->setStyleSheet(stylesheet);
        barTop->setIconSize(QSize(45, 45));
        barTop->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);
    
        QToolBar *barBottom = new QToolBar(this);
        barBottom->setMovable(false);
        barBottom->setFixedWidth(60);
        barBottom->setOrientation(Qt::Vertical);
        barBottom->setStyleSheet(stylesheet);
        barBottom->setIconSize(QSize(45, 45));
    
        QAction *a_Home = new QAction("Home");
        a_Home->setIcon(QIcon(":/icons/images/home.png"));
        a_Home->setToolTip("Übersicht");
        barTop->addAction(a_Home);
    
        QAction *a_Diary = new QAction("Diary");
        a_Diary->setIcon(QIcon(":/icons/images/diary.png"));
        a_Diary->setToolTip("Übersicht");
        barTop->addAction(a_Diary);
    
        QWidget *spacer = new QWidget(this);
        spacer->setMinimumHeight(300);
        spacer->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::MinimumExpanding);
        barTop->addWidget(spacer);
    
        QAction *a_Quit = new QAction("Quit");
        a_Quit->setIcon(QIcon(":/icons/images/quit.png"));
        a_Quit->setToolTip("Beenden");
        barBottom->addAction(a_Quit);
    
        QVBoxLayout *mainMenuLayout = new QVBoxLayout();
    
        mainMenuLayout->addWidget(barTop);
        mainMenuLayout->addWidget(barBottom);
    
        mainMenuLayout->setAlignment(barTop, Qt::AlignTop);
        mainMenuLayout->setAlignment(barBottom, Qt::AlignBottom);
        mainMenuLayout->setContentsMargins(0, 0, 0, 0);
        mainMenuLayout->setSpacing(0);
    
        QWidget *central = new QWidget(this);
        central->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
        central->setLayout(mainMenuLayout);
    
        setCentralWidget(central);
    
        this->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    
    1 Reply Last reply
    0
    • Fuel 0F Offline
      Fuel 0F Offline
      Fuel 0
      wrote on last edited by
      #2

      I moved the Code in an explizit QWidget Class and changed that now

      QWidget *spacer = new QWidget(this);
          spacer->setMinimumHeight(3000);
          spacer->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::MinimumExpanding);
          barTop->addWidget(spacer);
      

      It seems that it works. But the Solution to set the Height so big that the spacer goes out of screen isnt that clean. Any other Ideas?

      1 Reply Last reply
      0
      • Fuel 0F Offline
        Fuel 0F Offline
        Fuel 0
        wrote on last edited by
        #3

        Sorry for writing again, but i really dont like the Solution out of my last Post. Still have the Problem, that i cant expand my QToolBar. As i mentioned, i have added a QWidget as a Spacer to my QToolBar and added the QSizePolixy::Expanding, but that still dont work. Please help somebody.

        1 Reply Last reply
        0
        • Chris KawaC Offline
          Chris KawaC Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by Chris Kawa
          #4

          Hi, so you just want your top bar to stretch all the way down? If so then you overcomplicated it. You don't need any spacers. From your original code remove the following lines:

          barTop->setFixedHeight(200)
          

          It's obviously not gonna stretch if it has fixed height.

          mainMenuLayout->setAlignment(barTop, Qt::AlignTop);
          mainMenuLayout->setAlignment(barBottom, Qt::AlignBottom);
          

          Alignment is for placing widgets inside their layout cell, not in the layout itself. Setting Qt::AlignTop to the top toolbar means that it will not expand, but have the preferred size and stick to the top border of its cell in the layout. This is what's causing your problem.

          QWidget *spacer = new QWidget(this);
          spacer->setMinimumHeight(300);
          spacer->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::MinimumExpanding);
          barTop->addWidget(spacer);
          

          As I mentioned - this is not needed.

          central->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
          
          this->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
          

          These two are just unnecessary. There are no other widgets around so central will take any available space anyway and this is (I assume) the top level window so it's not even in a layout and this is pointless.

          Finally you need to set your policies correctly. You want your bottom bar to have preferred height and your top one to expand so set it up like this:

          barTop->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
          barBottom->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);
          
          1 Reply Last reply
          5
          • Fuel 0F Offline
            Fuel 0F Offline
            Fuel 0
            wrote on last edited by
            #5

            Thanks for your Help. Problem is solved.

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved