Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

add a "responsive" widget to toolbar



  • 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?


  • Lifetime Qt Champion

    Hi,

    Why are you fixing the width of your spacer ?



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



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



  • @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));
    }
    


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



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



  • 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?


Log in to reply