gpt4 book ai didi

c++ - QPushButton - 如何通过 PushButton 在行编辑框中附加文本字符串

转载 作者:行者123 更新时间:2023-11-30 03:40:52 25 4
gpt4 key购买 nike

我开始学习 Qt 和编码。我有一个基本的项目,用于练习目的。

这是我的小 UI 的图片:

New Screenshot

请耐心等待,因为我才刚开始使用 cpp。

我希望 QPushButton 在我单击后将此字符串 -> “文本”附加到文本框中。

点击两次会产生“texttext”等等。

我看到这个问题得到了回答: QT creating push buttons that add text to a text edit box

上面提到的解决方案似乎是我需要的,我只是不明白如何将它集成到我的项目中。

有没有人可以帮忙?

到目前为止我有这些文件:

测试.pro:

QT       += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = test
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui

主窗口.h :

class MainWindow : public QMainWindow
{
Q_OBJECT

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

private slots:
void addTextTolable();

private:
Ui::MainWindow *ui;
};

#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"

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

void MainWindow::addTextTolable()
{
ui->textEdit->appendPlainText("test");
}

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

这是我遇到的最后一个错误

mainwindow.cpp:-1: In member function 'void MainWindow::addTextTolable()':
mainwindow.cpp:14: error: 'class QTextEdit' has no member named 'appendPlainText'
ui->textEdit->appendPlainText("test");
^

最佳答案

欢迎使用 C++ 和 Qt 编码!这很有趣,但是有很多事情要发生。我会尽力修改你现有的东西来解释。从 QObject 继承的类通过 Qt 的信号/槽架构相互发送信号。所以,这就是您要做的。

1.) 在主窗口头文件中声明一个槽函数。这只是一个普通的函数声明,除了放在 slots: 选项卡下。

2.) 将来自 QPushButton 的“clicked(bool)”的信号连接到主窗口插槽,通常在 MainWindow 构造函数中

这是修改后的代码。

主窗口.h :

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
/*IMPORTANT NOTE: Q_OBJECT must appear in the beginning of the header of any object you want to use signals/slots for*/
Q_OBJECT

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

/*Declare the function to be called when the QPushButton is clicked*/
private slots:
void addTextToLabel();

private:
Ui::MainWindow *ui;
};

#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"

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

/*It's very important to note that doing anything with the ui object must be done AFTER ui->setupUi(this) is called. The program will segfault otherwise*/
/*General connect syntax:
connect(object that will emit signal, SIGNAL(signal emitted), object that will receive the signal, SLOT(slot function));
/*the pushButton is owned by the ui object*/
connect(ui->pushButton, SIGNAL(clicked(bool)), this, SLOT(addTextToLabel());
}

/*Now define the slot function*/
void MainWindow::addTextToLabel()
{
/*I actually can't tell from the UI whether the text box is a plainTextEdit or textEdit, so substitute the name of the text box (found in the QDesigner window)*/
ui->textEdit->appendText("test");
}

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

试一试,如果有任何问题或您还有其他问题,请告诉我。

关于c++ - QPushButton - 如何通过 PushButton 在行编辑框中附加文本字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37766470/

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