gpt4 book ai didi

c++ - QT按钮点击改变qplaintextedit

转载 作者:太空宇宙 更新时间:2023-11-04 13:43:51 27 4
gpt4 key购买 nike

我是 QT 的新手。我写了一个带有 4 个单选按钮、单击按钮和纯文本编辑字段的小表单(在 QT Creator 中)。我的目标是按下按钮并在纯文本编辑中进行文本更改(“此处有一些文本”)。不幸的是,我找不到连接按钮和文本字段的正确公式,所以我得到一个错误,我还不知道。这是代码(我省略了 GUI XML 部分):

主窗口.h:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QRadioButton>
#include <QPushButton>
#include <QPlainTextEdit>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

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

public slots:
void on_pushButton_clicked();

private:
Ui::MainWindow *ui;
QRadioButton radioButton,radioButton_2,radioButton_3,radioButton_4;
QPushButton pushButton;
QPlainTextEdit plainTextEdit;
};

#endif // MAINWINDOW_H

主要.cpp:

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

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

return a.exec();
}

主窗口.cpp:

#include "mainwindow.h"
#include "ui_mainwindow.h"
//#include <QPushButton>
//#include <QPlainTextEdit>

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QObject::connect(pushButton,SIGNAL(clicked()),plainTextEdit,SLOT(on_pushButton_clicked()));
}

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

void MainWindow::on_pushButton_clicked()
{
plainTextEdit.setPlainText("Some text here");
}

这是错误输出:

/home/user/workspace/ButtonRadio/mainwindow.cpp:-1: In constructor 'MainWindow::MainWindow(QWidget*)':
/home/user/workspace/ButtonRadio/mainwindow.cpp:11: error: no matching function for call to 'MainWindow::connect(QPushButton&, const char*, QPlainTextEdit&, const char*)'
/home/user/workspace/ButtonRadio/mainwindow.cpp:11: candidates are:
/usr/include/qt4/QtCore/qobject.h:204: static bool QObject::connect(const QObject*, const char*, const QObject*, const char*, Qt::ConnectionType)
/usr/include/qt4/QtCore/qobject.h:204: note: no known conversion for argument 1 from 'QPushButton' to 'const QObject*'
/usr/include/qt4/QtCore/qobject.h:217: static bool QObject::connect(const QObject*, const QMetaMethod&, const QObject*, const QMetaMethod&, Qt::ConnectionType)
/usr/include/qt4/QtCore/qobject.h:217: note: no known conversion for argument 1 from 'QPushButton' to 'const QObject*'
/usr/include/qt4/QtCore/qobject.h:337: bool QObject::connect(const QObject*, const char*, const char*, Qt::ConnectionType) const
/usr/include/qt4/QtCore/qobject.h:337: note: no known conversion for argument 1 from 'QPushButton' to 'const QObject*'

如果我只写

QObject::connect(pushButton,SIGNAL(clicked()),SLOT(on_pushButton_clicked()));

然后错误输出略有变化,但本质还是一样的。我怎么解决这个问题?

--编辑--15.11.14--

主窗口.h: ...

private:
Ui::MainWindow *ui;
QRadioButton *radioButton,*radioButton_2,*radioButton_3,*radioButton_4;
QPushButton *pushButton;
QPlainTextEdit *plainTextEdit;
};

主窗口.cpp:

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

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
pushButton(new QPushButton(this)),
plainTextEdit(new QPlainTextEdit(this))
{
ui->setupUi(this);
//pushButton = new QPushButton(this);
//plainTextEdit = new QPlainTextEdit(this);
QObject::connect(pushButton,SIGNAL(clicked()),plainTextEdit,SLOT(on_pushButton_clicked()));
}

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

void MainWindow::on_pushButton_clicked()
{
plainTextEdit->setPlainText("Some text here");
}

因此,我得到了具有以下输出的 SIGSEGV:

amember "1on_pushButton_clicked()" char *
asender @0x2 QObject
asignal "2clicked()" char *
atype Qt::AutoConnection(0) Qt::ConnectionType
this @0x7fffffffe640 QObject

它还指向 qobject.h 中的以下几行:

339 { return connect(asender, asignal, this, amember, atype); }
349 T qFindChild(const QObject *o, const QString &name = QString());
350 QList<T> qFindChildren(const QObject *oobj, const QString &name = QString());
351 QList<T> qFindChildren(const QObject *o, const QRegExp &re);

最佳答案

你得到这个错误是因为 connect 使用指针。你应该使用指针:

QRadioButton *radioButton,*radioButton_2,*radioButton_3,*radioButton_4;
QPushButton *pushButton;
QPlainTextEdit *plainTextEdit;

当您使用指针时,每个 . 替换为 ->。编译器通过下一个错误告诉您错误:

...(maybe you meant to use '->' ?) radioButton_2.show();

连接应该是:

connect(pushButton,SIGNAL(clicked()),this,SLOT(on_pushButton_clicked()));

connect(pushButton,SIGNAL(clicked()),SLOT(on_pushButton_clicked()));

关于 future :你也可以这样写:

QObject obj1;
QObject obj2;
connect(&obj1,SIGNAL(someSigna()),&obj2,SLOT(slot()));

如你所见,你需要&

您需要在 QObject 子类中编写 QObject::connect(例如在 main.cpp 中) 在您的 MainWindow 中 你可以只写 connect

此外,当您使用指针时,您应该使用new 分配内存。例如:

pushButton = new QPushButton(this);//in constructor
//and so on

this 告诉我们 this(我们的 MainWindow)是父级,所以 pushButton 将出现在 MainWindow,不在单独的窗口中。

我想我提到了这段代码中的所有重大错误。

on_pushButton_clicked() 是 mainwindow 的 slot,不是纯文本edit 下一步尝试:

pushButton = new QPushButton(this);
plainTextEdit = new QPlainTextEdit(this);
connect(pushButton,SIGNAL(clicked()),this,SLOT(on_pushButton_clicked()));

关于c++ - QT按钮点击改变qplaintextedit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26743275/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com