确定 Qt 中的对象类型

发布于 2024-08-20 12:12:51 字数 612 浏览 13 评论 0原文

我有一系列 QTextEdits 和 QLineEdits 通过 QSignalMapper 连接到插槽(它发出 textChanged(QWidget*) 信号)。当调用连接的插槽时(粘贴在下面),我需要能够区分两者,以便我知道是否调用 text() 或 toPlainText() 函数。确定 QWidget 子类类型的最简单方法是什么?

void MainWindow::changed(QWidget *sender)
{                   
   QTextEdit *temp = qobject_cast<QTextEdit *>(sender);
   QString currentText = temp->toPlainText(); // or temp->text() if its 
                                              // a QLineEdit...
   if(currentText.compare(""))
   {
      ...
   }
   else
   {
      ...
   }
}

我正在考虑使用 try-catch 但 Qt 似乎对异常没有非常广泛的支持......有什么想法吗?

I have a series of QTextEdits and QLineEdits connected to a slot through a QSignalMapper(which emits a textChanged(QWidget*) signal). When the connected slot is called (pasted below), I need to be able to differentiate between the two so I know whether to call the text() or toPlainText() function. What's the easiest way to determine the subclass type of a QWidget?

void MainWindow::changed(QWidget *sender)
{                   
   QTextEdit *temp = qobject_cast<QTextEdit *>(sender);
   QString currentText = temp->toPlainText(); // or temp->text() if its 
                                              // a QLineEdit...
   if(currentText.compare(""))
   {
      ...
   }
   else
   {
      ...
   }
}

I was considering using try-catch but Qt doesn't seem to have very extensive support for Exceptions... Any ideas?

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

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

发布评论

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

评论(2

春花秋月 2024-08-27 12:12:51

事实上,你的解决方案已经差不多了。事实上, qobject_cast 将返回 如果无法执行转换,则为 NULL。因此,在其中一个类上尝试,如果它为 NULL,则在另一个类上尝试:

QString text;
QTextEdit *textEdit = qobject_cast<QTextEdit*>(sender);
QLineEdit *lineEdit = qobject_cast<QLineEdit*>(sender);
if (textEdit) {
    text = textEdit->toPlainText();
} else if (lineEdit) {
    text = lineEdit->text();
} else {
    // Return an error
}

Actually, your solution is already almost there. In fact, qobject_cast will return NULL if it can't perform the cast. So try it on one of the classes, if it's NULL, try it on the other:

QString text;
QTextEdit *textEdit = qobject_cast<QTextEdit*>(sender);
QLineEdit *lineEdit = qobject_cast<QLineEdit*>(sender);
if (textEdit) {
    text = textEdit->toPlainText();
} else if (lineEdit) {
    text = lineEdit->text();
} else {
    // Return an error
}
难以启齿的温柔 2024-08-27 12:12:51

您还可以使用 sender->metaObject()->className() 这样就不会进行不必要的转换。特别是如果您有很多课程要测试。代码将是这样的:

QString text;
QString senderClass = sender->metaObject()->className();

if (senderClass == "QTextEdit") {
    QTextEdit *textEdit = qobject_cast<QTextEdit*>(sender);
    text = textEdit->toPlainText();
} else if (senderClass == "QLineEdit") {
    QLineEdit *lineEdit = qobject_cast<QLineEdit*>(sender);
    text = lineEdit->text();
} else {
    // Return an error
}

我知道这是一个老问题,但我留下这个答案以防万一它对某人有用......

You can also use sender->metaObject()->className() so you won't make unnecesary casts. Specially if you have a lot of classes to test. The code will be like this:

QString text;
QString senderClass = sender->metaObject()->className();

if (senderClass == "QTextEdit") {
    QTextEdit *textEdit = qobject_cast<QTextEdit*>(sender);
    text = textEdit->toPlainText();
} else if (senderClass == "QLineEdit") {
    QLineEdit *lineEdit = qobject_cast<QLineEdit*>(sender);
    text = lineEdit->text();
} else {
    // Return an error
}

I know is an old question but I leave this answer just in case it would be useful for somebody...

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