加载从信号槽接收到的 QStringList 值

Loading QStringList value received from signal slot

在我的qt c++ 应用程序中,一个QStringList 通过信号和槽机制从一个cpp 文件(MainWIndow) 发送到另一个cpp 文件(Dialog)!我想在加载界面时(没有单击按钮)在 Dialog.ui 的组合框中显示 qtringList 中的元素!

以下是我的代码

    #include "dialog.h"
    #include "ui_dialog.h"

    Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
    {
    ui->setupUi(this);

    for(int i=0;i<subMessages.size();i++){
    ui->comboBox->addItem(subMessages[i]);

    }
    }

//slot which is used to get the qstringList
    void Dialog::receiveSubMessages(QStringList List){ 
    subMessages=List;

    }

通过插槽成功接收到QStringList(已验证)。虽然我使用了for循环并尝试显示(如代码中所示),但组合框上没有显示任何内容!我该如何解决这个问题?

我做这个回答是为了向您展示如何解决您的问题。 (我感觉我什至不明白你的实际问题是什么。)

如果 MCVE is provided. (Please, follow this link. It teachs you really basic skills every S/W developer shouldmust have. I would also recommend to follow-up to How to debug small programs 提出问题,则获得有用答案的机会会增加。)

由于确实理解你的问题,所以我制作了这样一个MCVE。这是代码 testQComboBox:

#include <QtWidgets>

int main(int argc, char **argv)
{
  // build appl.
  qDebug() << "Qt Version:" << QT_VERSION_STR;
  QApplication app(argc, argv);
  // a QStringList with items
  QStringList items;
  items
    << QString::fromUtf8("first item")
    << QString::fromUtf8("second item")
    << QString::fromUtf8("third item");
  // build GUI
  QDialog dlg;
  QVBoxLayout vBox;
  QComboBox cmbBox;
  cmbBox.addItems(items);
  vBox.addWidget(&cmbBox);
  dlg.setLayout(&vBox);
  dlg.show();
  app.exec();
  // done
  return 0;
}

我在 Windows 10(64 位)上使用 Qt 5.9.2 在 VS2013 中编译它。这是它的样子:

如您所见,combobox 的使用非常简单——没有暗门可以使用。与QComboBox直接相关的实际代码正好是4行代码:

  QVBoxLayout vBox;
  QComboBox cmbBox;
  cmbBox.addItems(items);
  vBox.addWidget(&cmbBox);

并且只有一行代码将项目添加到 QComboBox:

  cmbBox.addItems(items);

注:

我使用 QComboBox::addItems() 而不是 QComboBox::addItem(),因为前者已经内置了一个循环来添加完整的 QStringList。它对您在代码中使用的循环没有任何影响 Dialog::Dialog().

所以,我终于敢做出如下声明:

如果您的组合框没有显示项目,那么:

  1. 您添加了空列表中的项目。

  2. 或者,您忘记添加列表中的项目。

  3. 或者,发生了很奇怪的事情。

我总是打赌 1. 或 2. 原因 – 3. 原因仅用于真正的紧急情况(例如损坏的 Qt 安装)。


关于3.原因:

我看到很多问题,其中显示了一些看起来无辜的代码行,这些代码看起来完全符合它们的要求,但声称会失败。最后几乎每次它都表明这些线在 MCVE but they didn't in the original code. How can this happen? Either there is some context which changes the behavior of the code in your original program or there is UB – undefined behavior. Something else does bad things but instead of crashing your process immediately (which would mean you're lucky) it goes on for a while corrupting the data more and more until finally everything breaks completely. Looking into the core-dump doesn't help at all. Therefore my recommendation of How to debug small programs.

中隔离时工作正常

为了获得工作代码,您需要将 for llop 放入插槽中:

#include "dialog.h"
#include "ui_dialog.h"

Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
    ui->setupUi(this);
}

//slot which is used to get the qstringList
void Dialog::receiveSubMessages(QStringList List){ 
    ui->comboBox->addItems (List);
}

如果你想在 Dialog 构造时用一些 QStringList 的内容填充组合框,那么你应该将此列表作为构造函数参数传递:

Dialog::Dialog(QStringList List, QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
    ui->setupUi(this); 
    ui->comboBox->addItems (List);
}

或在 Dialog 对象构建后立即调用 Dialog::receiveSubMessages()

// somewhere in code, where you construct Dialog object
// ...
auto parent = SomeQWidgetDerivedClass (...);
QStringList someStringList {
    "string 1",
    "string 2"
};
// ...
auto dialog {new Dialog ()};

dialog->receiveSubMessages (someStringList);
// ...

您提供的代码永远不会让您获得预期的结果,因为本应填充 QComboBoxfor 循环是在您的 Dialog 对象创建时执行的.那时你的 subMessages 是空的。您创建的插槽不会在构造函数之前调用 - 插槽只是一个成员函数,只能在创建对象后调用。如果函数本身是静态的,那么你只能调用没有对象的成员函数,在这种情况下它肯定不是槽。