简体   繁体   English

C ++-从QPushButton将项目添加到QListWidget

[英]C++ - Adding item to QListWidget from QPushButton

Hello I am trying to add items to a QListWidget from a QPushButton. 您好,我正在尝试从QPushButton将项目添加到QListWidget。 Both the QListWidget and QPushButton are added as individual widgets inside of a QGraphicsScene. QListWidget和QPushButton都作为单独的小部件添加到QGraphicsScene内。 I want the effect of a box that fills with text lines 我想要一个充满文本行的框的效果

main.c main.c中

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;

    QGraphicsView view;

    QGraphicsScene *scene = new QGraphicsScene(0, 0, 1200, 1200, &view);

    scene->setBackgroundBrush(Qt::gray);

    view.setScene(scene);

    QPushButton *PushButton1;
    PushButton1 = new QPushButton();
    PushButton1->setGeometry(QRect(19, 20, 154, 4));

    QListWidget *ListWidget;
    ListWidget = new QListWidget;

    scene->addWidget(ListWidget);
    scene->addWidget(PushButton1);

    QObject::connect(PushButton1, SIGNAL(clicked()),&w, SLOT(handleClick(*QListWidget)));

    view.show();

    return a.exec();

} }

mainwindow.cpp mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::handleClick(QListWidget *List)
{
    int test;

    List->addItem("TESTING");

    //QApplication::quit();

}

mainwindow.h mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QListWidget>


namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    Ui::MainWindow *ui;


    private slots:


    public slots:

        void handleClick(QListWidget *List);
};

#endif // MAINWINDOW_H

This code compiles fine. 此代码可以正常编译。 How I get the following error in the console when the application is running 应用程序运行时如何在控制台中出现以下错误

QObject::connect: No such slot MainWindow::handleClick(*ListWidget) in ..\MenuTest\main.cpp:48

Can someone help me do this? 有人可以帮我吗? I've seen several tutorials but it's using the designer to make the GUI and I'd like to know how to do it in code without using designer. 我看过几本教程,但是它使用设计器来制作GUI,我想知道如何在不使用设计器的情况下用代码来完成它。 Thanks. 谢谢。

Your slot accepts QListWidget but you're connecting with ListWidget as the parameter, the signature has to be an exact match due to the way signals and slots work in Qt. 您的插槽接受QListWidget,但您要使用ListWidget作为参数,由于信号和插槽在Qt中的工作方式,签名必须完全匹配。

Put handleClick under public slots: and change this line: 将handleClick放在public slots:并更改此行:

QObject::connect(PushButton1, SIGNAL(clicked()),&w, SLOT(handleClick(*ListWidget)));

To this: 对此:

QObject::connect(PushButton1, SIGNAL(clicked()),&w, SLOT(handleClick(*QListWidget)));

Update: 更新:

I see I missed a key point, the signatures have to match, as in parameter to parameter, so the line up there will not work. 我看到我错过了一个关键点,签名必须匹配,就像在参数之间一样,因此那里的行将无法工作。

To fix this remove the parameter completely, since PushButton1 can't send it automatically. 要解决此问题,请完全删除该参数,因为PushButton1无法自动发送该参数。

QObject::connect(PushButton1, SIGNAL(clicked()),&w, SLOT(handleClick()));

Also remove it here: 也可以在这里删除它:

void MainWindow::handleClick()

To access the QListWidget you'll have to reference it directly, either by passing it to MainWindow's constructor or iterating the window's controls. 要访问QListWidget,您必须直接引用它,方法是将其传递给MainWindow的构造函数或迭代窗口的控件。

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

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