我有一个主窗口,它有四个按钮,所有这四个按钮都连接到“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的值并且值没有存储在任何地方?你能让我知道我在这里失踪了吗?
答案 0 :(得分:0)
在每个按钮的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() );
}