繁体   English   中英

Qt错误:错误:没有匹配的函数调用'QHBoxLayout::addItem(QPushButton*&)'

[英]Qt error: error: no matching function for call to ‘QHBoxLayout::addItem(QPushButton*&)’

我正在尝试向 QHBox 添加项目,但我不断收到错误消息:

/media/root/5431214957EBF5D7/projects/c/qt/tools/plugandpaint/app/mainwindow.cpp:36: error: no matching function for call to ‘QHBoxLayout::addItem(QPushButton*&)’
     hlayout->addItem(m_button);
                              ^

我可能做错了什么?

主窗口.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H


#include <QHBoxLayout>

class PaintArea;

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow();

private slots:
    void handleButton();
    // Other slots

private:
    PaintArea *paintArea;
    QHBoxLayout *hlayout;

    // Other private items
};

#endif

主窗口.cpp

MainWindow::MainWindow() :
    paintArea(new PaintArea),
    scrollArea(new QScrollArea)
{

    // Create the button, make "this" the parent
    m_button = new QPushButton("My Button", this);
    // set size and location of the button
    m_button->setGeometry(QRect(QPoint(100, 100), QSize(200, 50)));

    // Connect button signal to appropriate slot
    connect(m_button, SIGNAL (released()), this, SLOT (handleButton()));

    hlayout = new QHBoxLayout;
    hlayout -> addItem(m_button);
    hlayout -> addItem(paintArea);

    scrollArea->setWidget(hlayout);

您应该改用addWidget()

它应该是:

hlayout->addWidget(m_button);

顺便说一句,按钮名称旁边的this不是必需的。 层次结构中的布局和它上面的小部件负责所有权。

暂无
暂无

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

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