使用 QPushButton 将文本添加到 QTextEdit

发布于 2024-11-14 00:13:12 字数 694 浏览 0 评论 0原文

只是一个简单的程序,用于在单击按钮时将文本添加到文本编辑...... 这里有什么问题吗?


#include<QPushButton>
#include<QApplication>
#include<QTextEdit>
#include<QWidget>
#include<QHBoxLayout>
#include<QLabel>

int main(int argc,char *argv[])
{
QApplication app(argc,argv);
QHBoxLayout *layout=new QHBoxLayout;
QTextEdit *text = new QTextEdit(); 
QWidget window;
QPushButton *button;

layout->addWidget(text);
button = new QPushButton();
button->setText(QChar(i+48));

QObject::connect(button,SIGNAL(clicked()),text,SLOT(setPlainText("hai")));

layout->addWidget(button);
window.setLayout(layout);
window.resize(500, 500);
window.show();

return app.exec();
}

Just a simple program to add text to textedit when button is clicked...
anything wrong here??


#include<QPushButton>
#include<QApplication>
#include<QTextEdit>
#include<QWidget>
#include<QHBoxLayout>
#include<QLabel>

int main(int argc,char *argv[])
{
QApplication app(argc,argv);
QHBoxLayout *layout=new QHBoxLayout;
QTextEdit *text = new QTextEdit(); 
QWidget window;
QPushButton *button;

layout->addWidget(text);
button = new QPushButton();
button->setText(QChar(i+48));

QObject::connect(button,SIGNAL(clicked()),text,SLOT(setPlainText("hai")));

layout->addWidget(button);
window.setLayout(layout);
window.resize(500, 500);
window.show();

return app.exec();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

生活了然无味 2024-11-21 00:13:12

您不能像这样使用 connect 。您无法将连接的SIGNAL中不存在的参数传递给SLOT

您需要将 clicked() 信号连接到您自己的插槽(不带参数),并自己调用 setPlainText 函数(或发出一个具有 <代码>QString参数)。

另一种选择是使用 QSignalMapper,如信号和槽高级中描述使用部分。

You can't use connect like that. You cannot pass parameters to the SLOT that are not present in the connected SIGNAL.

You will need to connect the clicked() signal to your own slot (with no argument), and call the setPlainText function yourself (or emit a new signal that has a QString parameter).

The other option is to use a QSignalMapper, as described in the Signals and Slots advanced usage section.

洋洋洒洒 2024-11-21 00:13:12

我认为这是行不通的,你不能在 connect 语句中为插槽提供默认参数。 SLOT 宏实际上只是将其参数转换为字符串,并在 text 类的已注册插槽列表中搜索插槽名称。

您必须调用自己的插槽,该插槽不带参数,并使用预期文本手动调用 setPlainText。也许 Qt 在这个方向上有一些帮助程序类,但你的解决方案不应该工作。

顺便问一下,您是否真的尝试过此操作并收到错误,或者您只是将其发布到此处而没有简单地尝试一下?

I think that won't work, you cannot give a slot a default argument inside the connect statement. The SLOT macros actually just transforms its argument into a string and searches for the slots name in a list of registered slots for the text class.

You have to call your own slot that takes no arguments and calls setPlainText manually with the intended text. Perhaps Qt has some helper class in that direction, but your solution should not work.

By the way, have you actually tried this and got an error, or have you just posted it here without simply trying it out?

迷鸟归林 2024-11-21 00:13:12

你的信号和插槽不兼容。 Qt 为此提供了文档。

尝试访问此页面

其中说,
“信号和槽机制是类型安全的:信号的签名必须与接收槽的签名相匹配。(事实上,槽的签名可能比它接收到的信号更短,因为它可以忽略额外的参数。)是兼容的,编译器可以帮助我们检测类型不匹配。信号和槽是松散耦合的:发出信号的类既不知道也不关心哪些槽接收信号”

信号槽机制可以与函数进行比较(或归结为)。称呼。您正在发出信号的地方调用函数(槽)。现在假设该函数需要一些参数并且没有默认值。但您正在尝试调用该函数。会发生什么?这就是您的代码中发生的情况。

Your signal and slot are incompatible. Qt provides doc for this .

Try to visit this page .

In this it says ,
"The signals and slots mechanism is type safe: The signature of a signal must match the signature of the receiving slot. (In fact a slot may have a shorter signature than the signal it receives because it can ignore extra arguments.) Since the signatures are compatible, the compiler can help us detect type mismatches. Signals and slots are loosely coupled: A class which emits a signal neither knows nor cares which slots receive the signal. "

Signal slot mechanism can be compared with (or boiled down to) function call. You are calling a function(slot) in a place where signal is emitted. Now imagine that the function expects some arguments and there is no default value. But you are trying to call the function. What will happen?. That's what happening in your code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文