- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我开始学习 Qt 和编码。我有一个基本的项目,用于练习目的。
这是我的小 UI 的图片:
请耐心等待,因为我才刚开始使用 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/
我正在创建一个 html 页面并想切换到页面的滚动和 float 。所以在正文或 div 中我想要一个元素列表。除非触摸到 div 的末尾,否则每个元素都应位于前一个元素的下方,然后它应该位于较高的部
我正在创建一个 html 页面并想切换到页面的滚动和 float 。所以在正文或 div 中我想要一个元素列表。除非触摸到 div 的末尾,否则每个元素都应位于前一个元素的下方,然后它应该位于较高的部
我有很多表,从我正在做选择的地方,但我只会写重要的部分。我有 2 张 table 住宿: id | title | desc | etc.. 住宿房间: id | accomodation_id |
我试图在数据框中找到相似的数字模式。我有一个包含 5 列的数据框,有些列的随机数介于 3 到 50 之间。但是,对于某些行,第 2 列或第 3 列没有数字。 A B C D E 5
我正在尝试使用 IntelliJ 在一个大型 java 文件中的第 65536 (2^16) 行之后安装断点,但事实证明这是不可能的。断点标有“X”,工具提示显示没有可执行代码。 是否有任何 Inte
我想在 bootstrap 表中并排显示两个图像,有什么想法吗? 这是代码, 7
我愿意: 从文件中读取行 查找包含指定文本的行(## Required reading) 如果行首包含星号 (*),则在带有特定文本的行之后打印下一行 如果下一行不再有带星号(*)的行(或不同的行),
我有以下 df Array = np.array([[87, 70, 95], [52, 47, 44], [44, 97, 94], [79, 36, 2]]) df_test
我有一个带有 onmouseover 功能的图像,它会在图像上显示一个带有一些选项的表格(图像变暗)。表格有一个 onmouseout 函数,它应该隐藏表格并再次显示图像。我遇到的问题是,每当鼠标在行
我有一个数组 A与形状 (N,)。我以 N=5 为例: A = np.array([0,1,1,0,1]) 我想将其转换为以下 NxN 矩阵 B . NumPy 和 Tensorflow 中的解决方案
预先感谢您的任何回复,这个论坛上的答案对我的研究非常宝贵。我是一名为了语言范式研究项目而学习 F# 的学生,我正在尝试利用 http://trelford.com/blog/post/specialk
这个问题在这里已经有了答案: Filter pandas DataFrame by substring criteria (17 个答案) 关闭 3 年前。 我有一个两列的数据框,一列是字符串,另一
我正在尝试学习 C 中的 malloc。我已经完成了一些 Java 编程,但想学习编码的具体细节。 让我解释一下我对此的了解,希望有人能填补空白。 行内: str = (char *) malloc(
我正在使用MVC3中的帮助程序Html.WebGrid,如何包括每个TR标签的数据? 我在列中有数据,我不想显示该列,并且我需要这些数据在表的TR上,然后使用JavaScript。 谢谢。 编辑 pu
我正在尝试将没有数据的数据库从 mysql 8 导出到 myqsl 5.7,但出现以下错误: Error 1726 (HY000) at line...: Storage engine 'InnoDB
我是一名优秀的程序员,十分优秀!