在 2 个窗口之间传递数据.Qt [英] Passing data between 2 windows. Qt

查看:30
本文介绍了在 2 个窗口之间传递数据.Qt的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是qt的新手,所以我不太了解信号槽机制.这是我的设置.Dialog 类(它是一个带有名为lineEdit"的 lineEdit 的对话框)mainwindow 类(也有一个 lineEdit)

I am new to qt, so I didn't quite get the signal slot mechanism. here is my setup. Dialog class (its a dialog with a lineEdit called "lineEdit") mainwindow class (that has a lineEdit too)

我有这个:

void MainWindow::keyPressEvent(QKeyEvent *event) {


    int i=event->key();
    //char z=(char)i;



   // connect(ui->lineEdit, SIGNAL(textChanged(QString)), dialog, SLOT(setText(QString)));

if(i>=48&&i<=57)

{
    QString s= QString::number(i-'0');


    q+=s;
    ui->lineEdit->setText(q);

}

我也想将对话框的 lineEdit 的文本设置为 q.我是怎么知道的?

I want to set the text of dialog's lineEdit to q too. how do I got about that ?

推荐答案

  1. 除非您了解基本原理,否则您不会使用 Qt.如果您不擅长理解文档,请阅读随附的大量示例代码.有些人更擅长阅读散文,有些人更擅长阅读代码,这并没有错.一定要这样做:)

  1. You will not go anywhere with Qt unless you understand the fundamentals. Read the plentiful example code that came with it if understanding documentation isn't your thing. Some people are better at reading prose, some at reading code, there's nothing wrong with that. Just be sure to do it :)

QLineEdit 已经处理了自己的击键.无需重新实现该功能.

A QLineEdit already processes its own keystrokes. There's no need to reimplement that functionality.

信号槽连接应该是静态的,除非您的应用程序正在改变状态.如果您多次将一个对象上的信号连接到另一个对象上的插槽,则 该插槽将被调用与连接数相同的次数.

Signal-slot connections should be static unless your application is changing states. If you connect a signal on an object to a slot on another object multiple times, then the slot will be called as many times as there are connections.

在一对QLineEdits之间传递数据的惯用方式如下:

The idiomatic way of passing data between a pair of QLineEdits is as follows:

connect(ui->lineEdit, SIGNAL(textEdited(QString)),
        dialog, SLOT(setText(QString)));
connect(dialog, SIGNAL(textEdited(QString)),
        ui->lineEdit, SLOT(setText(QString)));

您可能希望在 MainWindow 的构造函数中设置此连接,但无论如何您只希望它一次.

You'd probably want to set this connection up in the constructor of MainWindow, but in any case you only want it to be done once.

您应该使用 textEdited 信号,而不是 textChanged 信号.当用户与控件交互以更改它时,会发出前者.无论文本是由用户更改还是通过调用 setText 以编程方式发出,都会发出后者.如果你在一对控件之间将 textChanged 连接到 setText,你会得到一个无限循环.QML 足够聪明,可以检测到它,但小部件代码 AFAIK 不是.

You should be using the textEdited signal, not textChanged signal. The former is emitted when the user interacts with the control to change it. The latter is emitted whether the text was changed by the user or programmatically by calling setText. If you connected textChanged to setText between a pair of controls, you'd get an endless loop. QML is clever enough to detect it, but widgets code AFAIK isn't.

这篇关于在 2 个窗口之间传递数据.Qt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆