简体   繁体   English

添加到QToolBar时,用QtDesigner创建的QWidget不可见

[英]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 我创建了一个带有一堆QToolButtons的QWidget,并设法成功初始化了它,并使用一个单独的窗口将其显示

myWidget.setVisible(true)

My ultimate goal is however to add this widget to a QToolBar to show on Qt::LeftToolBarArea of my QMainWindow. 但是,我的最终目标是将此小部件添加到QToolBar上 ,以显示在QMainWindow的Qt :: LeftToolBarArea上。 I add myWidget to QToolBar using 我使用以下方式将myWidget添加到QToolBar

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). QToolBar已成功添加到我的QMainWindow中(我可以看到用于在QMainWindow的不同工具栏区域中移动它的手柄,并且可以在其中移动它)。 However my QWidget is not visible. 但是我的QWidget不可见。 I tried 我试过了

myToolBar.addWidget(myWidget).setVisible(true)

as specified by the manuals since setVisible() won't work unless called on a QAction. 如手册所述,因为setVisible()除非在QAction上调用,否则将不起作用。 I tried to add other pre-made widgets such as a QPushButton to my QToolBar and that is being visualized successfully. 我尝试将其他预制的小部件(例如QPushButton)添加到我的QToolBar中,并且已成功可视化。

Is there anything special that I need to do to my widget to make it visible in a QToolBar? 我需要对窗口小部件进行特殊处理以使其在QToolBar中可见吗?

Regards, 问候,

C 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: 因此,正如我所说,我使用qtDesigner创建了myWidget,因此可以向您展示创建的内容,希望它不会太长:

OnlineAssemblerPlayer.ui 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 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 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 还有Qt生成的* ui_OnlineAssemblerPlayer.h *

/********************************************************************************
** 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: 然后像下面这样在QMainWindow的构造函数中对其进行初始化:

OnlineAssemblerPlayer *mOnlineAssemblerPlayer = new OnlineAssemblerPlayer;

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

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

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? 您能否向我们展示代码“ myWidget”,以及从向工具栏添加小部件的更多内容?

EDIT 编辑

Try to add 尝试添加

setLayout(ui->horizontalLayout);

right after 之后

retranslateUi(OnlineAssemblerPlayer);

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 . 我偶然到类似的问题:创建自定义的widget( QWidget子类有一个成员QListView ),并试图将其添加到一个QToolBar我的QMainWindow It didn't show up no matter what I did despite being reparented to the QToolBar successfully. 尽管成功地重新加入了QToolBar但无论我做什么都没有显示。 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. 我没能在最后的工作,但想通了, QToolBar其实不是工作QWidgetsQActions :即使通过增加addWidget(WGT *)小部件只增加了一些用的QAction 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. 现在添加由一些默认小部件(例如QButton)组成的自定义小部件似乎很合逻辑,QToolBar只是不知道如何显示它。 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是为了自定义的widget功能添加到一个实现QToolBarQMenu等等-所有使用QActions QWidgetAction 's virtual functions createWidget(QWidget*) and deleteWidget(QWidget*) serve that goal. QWidgetAction的虚函数createWidget(QWidget*)deleteWidget(QWidget*)这一目标。

But in my case I avoided the problem: inherited not from QWidget , adding QListView in it, but directly from QListView . 但就我而言,我避免了这个问题:不是从QWidget继承而在其中添加QListView ,而是直接从QListView This way I could freely add it to the toolbar with its addWidget() . 这样,我可以使用其addWidget()将其自由添加到工具栏中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM