如何在主 QLabel 中包含小部件的子集例如 QListView 和 QLabels

问题描述

为了测试 QImage 的旋转,我创建了一个快速应用程序,其中我将 QLabel 子类化,如下所示。在这种情况下,基本上 QLabel 对我来说充当 MainWindow

以下是我的布局:

be

以下是我试图实现的布局:

be2

代码下方

ma​​inwindow.h

class MainWindow : public QLabel {
   Q_OBJECT
public:
   MainWindow(int argc,char** argv,QWidget *parent = {});
   virtual ~MainWindow();
protected:
   Q_SLOT void setimageMsg(const sensor_msgs::ImageConstPtr&);
   Q_SIGNAL void newImageMsg(const sensor_msgs::ImageConstPtr&);
private:
   ros::Subscriber sub;
   ros::Subscriber sub_img_bw;
};

ma​​inwindow.cpp

MainWindow::MainWindow(int argc,QWidget *parent) : QLabel(parent) {
   qRegisterMetaType<sensor_msgs::ImageConstPtr>();
#if QT_VERSION >= QT_VERSION_CHECK(5,0)
   connect(this,&MainWindow::newImageMsg,this,&MainWindow::setimageMsg);
#else
   connect(this,SIGNAL(newImageMsg(sensor_msgs::ImageConstPtr)),SLOT(setimageMsg(sensor_msgs::ImageConstPtr)));
#endif
   ros::init(argc,argv,"MainWindow");
   ros::NodeHandle n;
   QComboBox *comboColorBckgd = new QComboBox(this);
   QStringList sequence_len = QStringList() << tr("Bckgd Color") << tr("Green") << tr("Blue") << tr("Black");
   comboColorBckgd->addItems(sequence_len);

   QComboBox *comboColorBeam = new QComboBox(this);
   QStringList sequence_len_beam = QStringList() << tr("Beam Color") << tr("R") << tr("G") << tr("B");
   comboColorBeam->addItems(sequence_len_beam);

   qgridLayout *grid = new qgridLayout(this);
   grid->setColumnMinimumWidth(0,10);
   grid->addWidget(comboColorBckgd,Qt::AlignTop);
   grid->addWidget(comboColorBeam,1,Qt::AlignTop);
   comboColorBckgd->show();
   comboColorBeam->show();
 }

在这里面临的情况是,我不完全理解为什么我不能在子会话中划分主要 QLabel,其中:

一行有一个 QLabel一个 QListView 和 另一行有 'QListViewandQLabel`

如我试图实现的布局所示。

当然我尝试在函数中插入必要的组件

`MainWindow::MainWindow(int argc,QWidget *parent) : QLabel(parent) {

      // .. desired componentns here
}

尽管我能够添加所有组件,但我的最终布局非常混乱,尽管我确信我正确设置了 QLabel,但所有内容都属于单个 qgridLayout

也许我必须对 qgridLayout 进行子类化?我很困惑。

我不知道如何继续,我想提前感谢任何人阐明正在发生的事情或至少指出潜在的解决方案。

解决方法

我无法看到您拥有和正在尝试实现的布局的图像,所以我可能不完全理解您的问题,但是...

为什么必须对 QLabel 进行子类化?我想这会导致您的问题 - QLabel 不希望在其下方有子层次结构,因此它可能与 QLabel 发生冲突。

而不是这个父/子层次结构:

- MainWindow : public QLabel
-- QGridLayout
--- QComboBox
--- QComboBox

你试过了吗:

- MainWindow : public QWidget (or QMainWindow?)
-- QGridLayout
--- QLabel (top level)
--- QComboBox
--- QComboBox