假设我在Qt应用程序中有2种形式。 我需要在2种形式之间多次传输数据,因此我使用循环来完成此操作。 我只是想在Form 1继续循环之前从Form 2获取结果。 这是我想要的意图: 当我正常使用循环时,循环从i = 1开始并将数据发送到Form2。在Form 2将结果返回给Form ...
提示:本站收集StackOverFlow近2千万问答,支持中英文搜索,鼠标放在语句上弹窗显示对应的参考中文或英文, 本站还提供 中文繁体 英文版本 中英对照 版本,有任何建议请联系yoyou2525@163.com。
我有一个具有四个按钮的主窗口,所有这四个按钮都连接到“ clicked()”信号。 当我们单击按钮时,它会转到另一个替换主窗口的小部件(使用堆叠式布局)。 我想将按钮的名称传递给窗口小部件,以便可以使用该值并将其附加到窗口小部件中的标签上。 我尝试使用以下方法将值传递给Widgte:
mainwindow.h
public:
explicit MainWindow(QWidget *parent = 0);
QStackedLayout *stackedLayout;
QLineEdit* lineEdit();
mainwindow.cpp
QLineEdit* MainWindow::lineEdit()
{
//buttonName is a QLineEdit on the main form which is hidden and value is being set when a button is clicked
std::string text=ui->buttonName->text().toUtf8().constData();
std::cout<<"Button Name passed from main window is"<<text<<std::endl;
return ui->buttonName;
}
SecondForm.cpp
MainWindow mainWind;
std::string text=mainWind.lineEdit()->text().toUtf8().constData();
std::cout<<"The name of the button is"<<text<<std::endl;
这就是我所遵循的方法。 数据没有返回到第二种形式。 是否是因为我在单击按钮后设置了QLineEdit的值,并且该值未存储在任何地方? 你能告诉我我在这里想念的东西吗?
在每个按钮的on_button_clicked()插槽中,您可以将被单击的按钮的text()发送到新窗口小部件。
//Your signal declaration in MainWindow.h
void signalToWidget( QString buttonText );
/*The slot in your widget to handle the text (you may need to subclass your widget to add this functionality*/
void slotSetText( QString buttonText )
{
widgetElement->setText( buttonText );
}
//connect in MainWindow constructor
QObject::connect( MainWindow, SIGNAL( signalToWidget( QString ) ),
widget, SLOT( slotSetText( QString ) ) );
void MainWindow::onButton1Clicked()
{
emit signalToWidget( ui->button1->text() );
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.