【发布时间】:2018-11-02 17:30:16
【问题描述】:
我使用 qtcreator 创建了一个在 QMainWindow 中运行的应用程序,所以是典型的方式。
我在状态栏中添加了两个“手动”(意思是:不使用表单编辑器)创建的 qlabel:
在标题中:
QLabel *label_timestamp;
QLabel *contentLabel_timestamp;
在构造函数中:
MainWin::MainWin(const CmdLineOptions &opts, QWidget *parent)
: QMainWindow(parent),
ui(new Ui::MainWin),
m_connectionStatusLabel(new QLabel),
m_client(new QMqttClient),
m_mqttmanager(new MQTTManager(m_client)),
m_mqttServerName("localhost")
{
ui->setupUi(this);
label_timestamp = new QLabel(this);
contentLabel_timestamp = new QLabel(this);
label_timestamp->setText("system time");
contentLabel_timestamp->setText("dd.mm.yyyy, hh:mm:ss:zzz"); /* just testing output */
statusBar()->addPermanentWidget(label_timestamp);
statusBar()->addPermanentWidget(contentLabel_timestamp);
}
如果我这样做
Label *label = findChild<QLabel *>(QString("contentLabel_")+objName);
在这个类实现的其他地方,objName 是'timestamp',当然,findChild() 返回 0。它与在表单编辑器中使用 QtCreator 创建的其他 QLabel 工作正常,findChild() 可以找到它们。状态栏小部件及其内容不也是 ui 的子项吗?有人最终知道出路吗?
我想使用 findChild 来按照命名方案用我通过 MQTT 接收的内容来一般地填充我的标签,这就是背景。如果状态栏内容需要特殊处理但也可以通过这种动态方法进行处理,那就太好了。
非常感谢
【问题讨论】: