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. add a "responsive" widget to toolbar
QtWS25 Last Chance

add a "responsive" widget to toolbar

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 1.2k 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.
  • U Offline
    U Offline
    user4592357
    wrote on last edited by
    #1

    i have a toolbar widget to which i've added a couple of actions.
    i also want to add some text to the toolbar. so i create a label. but i want this label to be positioned at the center of the toolbar, so:

    Toolbar::Toolbar(const QString &sToolbarName, QWidget *parent /* = nullptr */) noexcept
    	: QToolbar(sToolbarName, parent)
    	, m_pLabel(new QLabel(this))
    {
    	// add actions
    
    	auto pSpacer = new QWidget(this);
    	pSpacer->setFixedWidth(width() / 2);
    	addWidget(pSpacer);
    
    	m_pLabel->setVisible(true);
    	addWidget(m_pLabel);
    }
    

    however when the toolbar is resized the label remains in its old place. i want this label to respond to resizing. what should i do?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Why are you fixing the width of your spacer ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      U 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        Why are you fixing the width of your spacer ?

        U Offline
        U Offline
        user4592357
        wrote on last edited by
        #3

        @SGaist
        i wasn't sure how else to do it

        1 Reply Last reply
        0
        • qwasder85Q Offline
          qwasder85Q Offline
          qwasder85
          wrote on last edited by qwasder85
          #4
          • Put the label in a layout and align it in the center (Qt::AlignHCenter, or Qt::AlignCenter). Don't forget to also align the text within the label in its center.
          • Assign the layout to a widget.
          • Set the widget's sizePolicy: setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred)
          • Add the widget to the toolbar

          The widget should now span the entire available space and the label within it should stay centered.

          Edit: Here's some test code:

              QLabel* p_label = new QLabel("Hello");
              p_label->setAlignment(Qt::AlignCenter);
          
              QHBoxLayout* p_tool_layout = new QHBoxLayout;
              p_tool_layout->addWidget(p_label, Qt::AlignCenter);
          
              QWidget* p_wid = new QWidget;
              p_wid->setContentsMargins(0, 0, 0, 0);
              p_wid->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
              p_wid->setLayout(p_tool_layout);
          
              QToolButton* p_butt_1 = new QToolButton;
              QToolButton* p_butt_2 = new QToolButton;
              QToolButton* p_butt_3 = new QToolButton;
          
              setStyleSheet("QToolButton { border: 1px solid magenta; }"); // Just for visibility in this example
          
              QToolBar* p_bar = new QToolBar;
              p_bar->addWidget(p_butt_1);
              p_bar->addWidget(p_butt_2);
              p_bar->addWidget(p_wid);
              p_bar->addWidget(p_butt_3);
          

          Result:
          alt text

          The layout is responsible for keeping the label centered between the tool buttons, and does so even when resizing.

          U 1 Reply Last reply
          2
          • qwasder85Q qwasder85
            • Put the label in a layout and align it in the center (Qt::AlignHCenter, or Qt::AlignCenter). Don't forget to also align the text within the label in its center.
            • Assign the layout to a widget.
            • Set the widget's sizePolicy: setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred)
            • Add the widget to the toolbar

            The widget should now span the entire available space and the label within it should stay centered.

            Edit: Here's some test code:

                QLabel* p_label = new QLabel("Hello");
                p_label->setAlignment(Qt::AlignCenter);
            
                QHBoxLayout* p_tool_layout = new QHBoxLayout;
                p_tool_layout->addWidget(p_label, Qt::AlignCenter);
            
                QWidget* p_wid = new QWidget;
                p_wid->setContentsMargins(0, 0, 0, 0);
                p_wid->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
                p_wid->setLayout(p_tool_layout);
            
                QToolButton* p_butt_1 = new QToolButton;
                QToolButton* p_butt_2 = new QToolButton;
                QToolButton* p_butt_3 = new QToolButton;
            
                setStyleSheet("QToolButton { border: 1px solid magenta; }"); // Just for visibility in this example
            
                QToolBar* p_bar = new QToolBar;
                p_bar->addWidget(p_butt_1);
                p_bar->addWidget(p_butt_2);
                p_bar->addWidget(p_wid);
                p_bar->addWidget(p_butt_3);
            

            Result:
            alt text

            The layout is responsible for keeping the label centered between the tool buttons, and does so even when resizing.

            U Offline
            U Offline
            user4592357
            wrote on last edited by
            #5

            @qwasder85
            thanks, this works like magic!

            one problem i have is that, since i'm adding this label widget to toolbar, when the toolbar gets narrower and a drop down list is created for the other buttons, this label is not in that list.
            i tried to use widget action, now the drop down list is populated with the label, but no text is visible.

            class NameWidgetAction : public QWidgetAction
            {
            public:
            	explicit NameWidgetAction(QObject *parent = nullptr);
            	void setName(const QString &sName);
            
            protected:
            	QWidget *createWidget(QWidget *parent) override;
            
            private:
            	/// The name label
            	QLabel *m_pNameLabel = nullptr;
            
            }; // class NameWidgetAction
            
            NameWidgetAction::NameWidgetAction(QObject *parent /* = nullptr */)
            	: QWidgetAction(parent)
            {
            }
            
            void NameWidgetAction::setName(const QString &sName)
            {
            	// get the list of created widgets, there should be just one
            	auto lstWidgets = createdWidgets();
            	if (!lstWidgets.isEmpty())
            	{
            		if (auto pLabel = lstWidgets[0]->findChild<QLabel *>())
            			pLabel->setText(sName);
            	}
            }
            
            QWidget *NameWidgetAction::createWidget(QWidget *parent)
            {
            	auto pWideWidget = new QWidget(parent);
            	pWideWidget->setContentsMargins(0, 0, 0, 0);
            	pWideWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
            
            	auto m_pDbNameLabel = new QLabel(pWideWidget);
            	m_pDbNameLabel->setAlignment(Qt::AlignCenter);
            	m_pDbNameLabel->setVisible(true);
            
            	auto pLayout = new QHBoxLayout(pWideWidget);
            	pLayout->addWidget(m_pDbNameLabel);
            
            	return pWideWidget;
            }
            

            and then in my toolbar i do:

            Toolbar::Toolbar(const QString &sToolbarName, QWidget *parent /* = nullptr */) noexcept
            	: QToolbar(sToolbarName, parent)
            {
                // ...
            	addAction(new NameWidgetAction(this));
            }
            
            qwasder85Q 1 Reply Last reply
            0
            • U user4592357

              @qwasder85
              thanks, this works like magic!

              one problem i have is that, since i'm adding this label widget to toolbar, when the toolbar gets narrower and a drop down list is created for the other buttons, this label is not in that list.
              i tried to use widget action, now the drop down list is populated with the label, but no text is visible.

              class NameWidgetAction : public QWidgetAction
              {
              public:
              	explicit NameWidgetAction(QObject *parent = nullptr);
              	void setName(const QString &sName);
              
              protected:
              	QWidget *createWidget(QWidget *parent) override;
              
              private:
              	/// The name label
              	QLabel *m_pNameLabel = nullptr;
              
              }; // class NameWidgetAction
              
              NameWidgetAction::NameWidgetAction(QObject *parent /* = nullptr */)
              	: QWidgetAction(parent)
              {
              }
              
              void NameWidgetAction::setName(const QString &sName)
              {
              	// get the list of created widgets, there should be just one
              	auto lstWidgets = createdWidgets();
              	if (!lstWidgets.isEmpty())
              	{
              		if (auto pLabel = lstWidgets[0]->findChild<QLabel *>())
              			pLabel->setText(sName);
              	}
              }
              
              QWidget *NameWidgetAction::createWidget(QWidget *parent)
              {
              	auto pWideWidget = new QWidget(parent);
              	pWideWidget->setContentsMargins(0, 0, 0, 0);
              	pWideWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
              
              	auto m_pDbNameLabel = new QLabel(pWideWidget);
              	m_pDbNameLabel->setAlignment(Qt::AlignCenter);
              	m_pDbNameLabel->setVisible(true);
              
              	auto pLayout = new QHBoxLayout(pWideWidget);
              	pLayout->addWidget(m_pDbNameLabel);
              
              	return pWideWidget;
              }
              

              and then in my toolbar i do:

              Toolbar::Toolbar(const QString &sToolbarName, QWidget *parent /* = nullptr */) noexcept
              	: QToolbar(sToolbarName, parent)
              {
                  // ...
              	addAction(new NameWidgetAction(this));
              }
              
              qwasder85Q Offline
              qwasder85Q Offline
              qwasder85
              wrote on last edited by
              #6

              @user4592357 Just a guess, but could it be that the dropdown only displays the QAction's icons and nothing else?

              U 1 Reply Last reply
              0
              • qwasder85Q qwasder85

                @user4592357 Just a guess, but could it be that the dropdown only displays the QAction's icons and nothing else?

                U Offline
                U Offline
                user4592357
                wrote on last edited by
                #7

                @qwasder85
                i'm not sure. but i also set iconText to the name widget action and it still wasn't displayed in drop down.

                1 Reply Last reply
                0
                • U Offline
                  U Offline
                  user4592357
                  wrote on last edited by user4592357
                  #8

                  also, the buttons that actually appear in the drop-down list, become disabled in the list (but i do not explicitly disable them), and when i widen the toolbar and actions come back to the toolbar, they're enabled again.
                  what's the reason?

                  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