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

时间:2017-02-04 14:09:22

标签: c++ qt

我正在尝试将项目添加到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);
                              ^

我可能做错了什么?

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

mainwindow.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);

2 个答案:

答案 0 :(得分:1)

您应该使用addWidget()代替。

答案 1 :(得分:0)

应该是:

hlayout->addWidget(m_button);

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