user1501973
user1501973

Reputation: 13

QWidget created with QtDesigner is not visible when added to QToolBar

I have created a QWidget with a bunch of QToolButtons in it and I managed to successfully initialize it and show it as a separate window using

myWidget.setVisible(true)

My ultimate goal is however to add this widget to a QToolBar to show on Qt::LeftToolBarArea of my QMainWindow. I add myWidget to QToolBar using

myToolBar.addWidget(myWidget)

The QToolBar is successfully added to my QMainWindow (I can see the handle used to move it around the different toolbar areas of my QMainWindow and can move it around). However my QWidget is not visible. I tried

myToolBar.addWidget(myWidget).setVisible(true)

as specified by the manuals since setVisible() won't work unless called on a QAction. I tried to add other pre-made widgets such as a QPushButton to my QToolBar and that is being visualized successfully.

Is there anything special that I need to do to my widget to make it visible in a QToolBar?

Regards,

C

<< EDIT >>

So, as I said, I created myWidget using qtDesigner so I can show you what that created and hopefully is not too long:

OnlineAssemblerPlayer.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>OnlineAssemblerPlayer</class>
 <widget class="QWidget" name="OnlineAssemblerPlayer">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>211</width>
    <height>30</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Form</string>
  </property>
  <widget class="QWidget" name="horizontalLayoutWidget">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>211</width>
     <height>31</height>
    </rect>
   </property>
   <layout class="QHBoxLayout" name="horizontalLayout">
    <item>
     <widget class="QToolButton" name="toolButton_2">
      <property name="text">
       <string>...</string>
      </property>
     </widget>
    </item>
    <item>
     <widget class="QToolButton" name="toolButton">
      <property name="text">
       <string>...</string>
      </property>
     </widget>
    </item>
    <item>
     <widget class="QComboBox" name="comboBox"/>
    </item>
   </layout>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>

OnlineAssemblerPlayer.h

#ifndef ONLINEASSEMBLERPLAYER_H
#define ONLINEASSEMBLERPLAYER_H

#include <QWidget>

namespace Ui {
class OnlineAssemblerPlayer;
}

class OnlineAssemblerPlayer : public QWidget
{
    Q_OBJECT

public:
    explicit OnlineAssemblerPlayer(QWidget *parent = 0);
    ~OnlineAssemblerPlayer();

private:
    Ui::OnlineAssemblerPlayer *ui;
};

#endif // ONLINEASSEMBLERPLAYER_H

OnlineAssemblerPlayer.cc

#include "OnlineAssemblerPlayer.h"
#include "ui_OnlineAssemblerPlayer.h"

OnlineAssemblerPlayer::OnlineAssemblerPlayer(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::OnlineAssemblerPlayer)
{
    ui->setupUi(this);
}

OnlineAssemblerPlayer::~OnlineAssemblerPlayer()
{
    delete ui;
}

And the *ui_OnlineAssemblerPlayer.h* generated by Qt

/********************************************************************************
** Form generated from reading UI file 'OnlineAssemblerPlayer.ui'
**
** Created: Wed Jul 4 16:23:39 2012
**      by: Qt User Interface Compiler version 4.8.1
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/

#ifndef UI_ONLINEASSEMBLERPLAYER_H
#define UI_ONLINEASSEMBLERPLAYER_H

#include <QtCore/QVariant>
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QButtonGroup>
#include <QtGui/QComboBox>
#include <QtGui/QHBoxLayout>
#include <QtGui/QHeaderView>
#include <QtGui/QToolButton>
#include <QtGui/QWidget>

QT_BEGIN_NAMESPACE

class Ui_OnlineAssemblerPlayer
{
public:
    QWidget *horizontalLayoutWidget;
    QHBoxLayout *horizontalLayout;
    QToolButton *toolButton_2;
    QToolButton *toolButton;
    QComboBox *comboBox;

    void setupUi(QWidget *OnlineAssemblerPlayer)
    {
        if (OnlineAssemblerPlayer->objectName().isEmpty())
            OnlineAssemblerPlayer->setObjectName(QString::fromUtf8("OnlineAssemblerPlayer"));
        OnlineAssemblerPlayer->resize(211, 30);
        horizontalLayoutWidget = new QWidget(OnlineAssemblerPlayer);
        horizontalLayoutWidget->setObjectName(QString::fromUtf8("horizontalLayoutWidget"));
        horizontalLayoutWidget->setGeometry(QRect(0, 0, 211, 31));
        horizontalLayout = new QHBoxLayout(horizontalLayoutWidget);
        horizontalLayout->setObjectName(QString::fromUtf8("horizontalLayout"));
        horizontalLayout->setContentsMargins(0, 0, 0, 0);
        toolButton_2 = new QToolButton(horizontalLayoutWidget);
        toolButton_2->setObjectName(QString::fromUtf8("toolButton_2"));

        horizontalLayout->addWidget(toolButton_2);

        toolButton = new QToolButton(horizontalLayoutWidget);
        toolButton->setObjectName(QString::fromUtf8("toolButton"));

        horizontalLayout->addWidget(toolButton);

        comboBox = new QComboBox(horizontalLayoutWidget);
        comboBox->setObjectName(QString::fromUtf8("comboBox"));

        horizontalLayout->addWidget(comboBox);


        retranslateUi(OnlineAssemblerPlayer);

        QMetaObject::connectSlotsByName(OnlineAssemblerPlayer);
    } // setupUi

    void retranslateUi(QWidget *OnlineAssemblerPlayer)
    {
        OnlineAssemblerPlayer->setWindowTitle(QApplication::translate("OnlineAssemblerPlayer", "Form", 0, QApplication::UnicodeUTF8));
        toolButton_2->setText(QApplication::translate("OnlineAssemblerPlayer", "...", 0, QApplication::UnicodeUTF8));
        toolButton->setText(QApplication::translate("OnlineAssemblerPlayer", "...", 0, QApplication::UnicodeUTF8));
    } // retranslateUi

};

namespace Ui {
    class OnlineAssemblerPlayer: public Ui_OnlineAssemblerPlayer {};
} // namespace Ui

QT_END_NAMESPACE

#endif // UI_ONLINEASSEMBLERPLAYER_H

Then I initialize it in the constructor of my QMainWindow like this:

OnlineAssemblerPlayer *mOnlineAssemblerPlayer = new OnlineAssemblerPlayer;

QToolBar *mToolbarAssemblerPlayer = new QToolBar(tr("AssemblerPlayer"));

mToolbarAssemblerPlayer->addWidget(mOnlineAssemblerPlayer);
mToolbarAssemblerPlayer->setMovable(true);
mToolbarAssemblerPlayer->setAllowedAreas(Qt::AllToolBarAreas);
addToolBar(Qt::LeftToolBarArea, mToolbarAssemblerPlayer);

Upvotes: 1

Views: 1611

Answers (2)

alexey
alexey

Reputation: 441

I stumbled into similar problem: created a custom widget (QWidget subclass with a member QListView) and tried to add it to a QToolBar of my QMainWindow. It didn't show up no matter what I did despite being reparented to the QToolBar successfully. Though simple default widgets (button, textedits and even listview) were visible and working nicely.

I didn't make it work in the end, but figured out that QToolBar actually operates not with QWidgets but with QActions: even adding widget through addWidget(wgt*) it only adds some QAction connected with wgt.

Now it seems logical that adding custom widget consisting of some default widgets like QButton, the QToolBar just doesn't know how to display it. As I understand it's solved with QWidgetAction that is implemented in order to add custom widget functionality to a QToolBar, QMenu etc - all that uses QActions. QWidgetAction's virtual functions createWidget(QWidget*) and deleteWidget(QWidget*) serve that goal.

But in my case I avoided the problem: inherited not from QWidget, adding QListView in it, but directly from QListView. This way I could freely add it to the toolbar with its addWidget().

Upvotes: 0

Blood
Blood

Reputation: 4186

You don't have to set visible of your toolbar. Are you sure that you add anything to your widget look? I'm almost sure that you didn't set any widget look. Can you show us code of 'myWidget' and something more from adding widget to toolbar?

EDIT

Try to add

setLayout(ui->horizontalLayout);

right after

retranslateUi(OnlineAssemblerPlayer);

Upvotes: 1

Related Questions