【问题标题】:How to create a QWidget如何创建一个 QWidget
【发布时间】:2017-02-06 17:32:20
【问题描述】:

我正在尝试创建 QWidget,但我不断收到错误消息:

/media/root/5431214957EBF5D7/projects/c/qt/tools/plugandpaint/app/mainwindow.cpp:53: error: no matching function for call to ‘QWidget::setLayout(QScrollArea*&)’
     mainWin -> setLayout(scrollArea);
                                    ^

ma​​inwindow.cpp

#include "mainwindow.h"

#include <QScrollArea>
#include <QApplication>

MainWindow::MainWindow() :
    scrollArea(new QScrollArea)
{
    mainWin = new QWidget();

    // 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()));

    label = new QLabel(QApplication::translate("windowlayout", "Name:"));
    lineEdit = new QLineEdit();

    layout = new QHBoxLayout();
    layout->addWidget(label);
    layout->addWidget(lineEdit);

    scrollArea->setLayout(layout);

    mainWin -> setLayout(scrollArea);
}

ma​​inwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QPushButton>
#include <QHBoxLayout>
#include <QLabel>
#include <QLineEdit>

class QActionGroup;
class QScrollArea;

class MainWindow : public QWidget
{
    Q_OBJECT

public:
    MainWindow();

private:

    QWidget *mainWin;
    QScrollArea *scrollArea;
    QStringList pluginFileNames;

    QPushButton *m_button;
    QLabel *label;
    QLineEdit *lineEdit;
    QHBoxLayout *layout;

    QVBoxLayout *vBox;
};

#endif

以下是我尝试将所有内容组合在一起的方法:

#include "mainwindow.h"
#include <QtPlugin>
#include <QApplication>

#include <QDesktopWidget>

Q_IMPORT_PLUGIN(BasicToolsPlugin)

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    MainWindow window;

    QDesktopWidget dw;

    int x=dw.width()*0.7;
    int y=dw.height()*0.7;
    window.setFixedSize(x,y);

    window.show();


    return app.exec();
}

我做错了什么?我完全是 Qt/C++ 新手,如果问题太明显的话。

提前谢谢你。

【问题讨论】:

  • 请阅读手册。警告说明了一切:qwidget 没有将布局设置为滚动区域的方法。
  • setLayout 请求一个 QLayout 指针,你给它一个 QScrollArea 指针。编译器不喜欢这样。
  • -1 很抱歉,这个问题与您的问题完全不符,而且很难说出您想要完成的任务,方法是将 Windows 的布局设置为...滚动区域。请将其压缩为 MCVE,缩小问题范围,阅读文档等。

标签: c++ qt qwidget


【解决方案1】:

首先,我看不到任何地方

new QScrollearea()

第二,这个:

QScrollArea*&

尤其是

*&

大多数时候是指针/引用不匹配的提示

【讨论】:

    猜你喜欢
    • 2010-09-22
    • 2020-10-14
    • 2012-03-02
    • 2021-05-13
    • 1970-01-01
    • 2012-05-10
    • 1970-01-01
    • 2011-07-28
    • 1970-01-01
    相关资源
    最近更新 更多