Qt多个ui界面,如何建立联系

一、最简单的方法:

无非就是你建了多个ui界面, 然后你只需要new它,获得它的地址信息,就可以建立联系了,如下

在MainWindow.c添加,然后MainWindow.c就可以跟test1和test2建立联系了

    testOne = new test1;
    testTwo = new test2;
    testTwo->setWindowTitle("test2");
    testOne->setWindowTitle("test1");
    connect(this,SIGNAL(sendTest1(QString)),testOne,SLOT(setText(QString)));
    connect(this,SIGNAL(sendTest2(QString)),testTwo,SLOT(setText(QString)));

再添加两个按键

void MainWindow::on_pushButton_clicked(){

testOne->show();

emit sendTest1("this is test1");}

void MainWindow::on_pushButton_2_clicked(){

testTwo->show();

emit sendTest2("this is test2");}

效果为:

二、如果还有其他函数想链接到test1和test2函数,而且你还不可以在new test1和test2,你可以以MainWindow.c为中介,发信号给MainWindow.c,因为MainWindow.c早就new了test,所以就可以发信号了,但是这样就会显得繁琐,好处了减少了内存消耗,先新建一个test3,

test3里面只有两个按键,发信号给mainwindos,


void test3::on_pushButton_clicked()
{
    emit showtest1("通过mainw显示test1");
}

void test3::on_pushButton_2_clicked()
{
    emit showtest2("通过mainw显示test2");
}
在mainwindows里面关联这个信号,
    testThree = new test3;
    testThree->setWindowTitle("test3");
    connect(testThree,SIGNAL(showtest1(QString)),this,SLOT(mTshowTest1(QString)));
    connect(testThree,SIGNAL(showtest2(QString)),this,SLOT(mTshowTest2(QString)));
void MainWindow::mTshowTest1(const QString & text1)
{

    testOne->show();
    emit sendTest1(text1);
}

void MainWindow::mTshowTest2(const QString & text2)
{
    testTwo->show();
    emit sendTest2(text2);
}

效果为:

这样就可以啦,

有需要的可以下载啊https://download.csdn.net/download/qq_41399894/10971923,千万没下,这个是留给我自己的,现在不能自己选c币好烦

猜你喜欢

转载自blog.csdn.net/qq_41399894/article/details/87460230