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

时间:2016-06-11 17:12:31

标签: c++ qt

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

这是我的小UI的照片:

New Screenshot

请耐心等待我,因为我刚刚开始参加cpp。

我希望QPushButton附加此字符串 - >单击后,文本框内的“文本”。

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

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

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

有没有人可以提供帮助?

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

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

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

main.cpp中:

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

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

    return a.exec();
}

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

1 个答案:

答案 0 :(得分:2)

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

1。)在主窗口头文件中声明一个槽函数。这只是一个普通的函数声明,除了放在slot:tab。

之外

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

所以这是修改后的代码。

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

main.cpp中:

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

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

    return a.exec();
}

和mainwindow.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;
}

如果出现任何问题,或者您还有其他问题,请告诉我。