首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何打印QLabel?

如何打印QLabel?
EN

Stack Overflow用户
提问于 2017-03-27 10:14:24
回答 1查看 1K关注 0票数 0

我正在尝试从QT中的一个组合框打印一个QLabel。代码如下:

代码语言:javascript
复制
QApplication a(argc, argv);
QWidget w;

QVBoxLayout *layout = new QVBoxLayout(&w);
QLabel *label = new QLabel("Here you will see the selected text from ComboBox", &w);
QComboBox *combo = new QComboBox(&w);
layout->addWidget(label);
layout->addWidget(combo);
Q_FOREACH(QSerialPortInfo port, QSerialPortInfo::availablePorts()) {
    combo->addItem(port.portName());

QObject::connect(combo, SIGNAL(currentIndexChanged(QString)), label, (SLOT(setText(QString))));

如何通过cout打印标签?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-03-27 10:37:48

您的代码似乎在使用Qt4,让我们将其移植到Qt5和较新的C++,好吗?

代码语言:javascript
复制
#include <QtWidgets>
#include <QtSerialPort>

int main(int argc, char ** argv) {
   QApplication app(argc, argv);
   QWidget w;

   auto layout = new QVBoxLayout(&w);
   auto label = new QLabel("Here you will see the selected text from ComboBox");
   auto combo = new QComboBox;
   layout->addWidget(label);
   layout->addWidget(combo);
   for (auto port : QSerialPortInfo::availablePorts())
       combo->addItem(port.portName());

   QObject::connect(combo, &QComboBox::currentTextChanged, [label, combo](){
       label->setText(combo->currentText());
       qDebug() << combo->currentText();
   });

   w.show();
   return app.exec();
}
  • 尝试在新代码中不使用Q_FOREACH,它会很可能会在未来被移除
  • 当新运算符已经指定类型时使用auto,这简化了代码,
  • 使用qDebug将调试信息输出到终端,
  • 当调用的代码很短时,在连接中使用lambdas,
  • 新型连接用于连接,因为它们将保证您的代码实际工作,旧样式具有运行时检查,而新样式具有构建时间检查。
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43043674

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档