求解决方案
以下是代码:
MainWindow.cpp
#include "mainwindow.h"
#include "mytest.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
label = new QLabel(this);
QPalette palette;
palette.setBrush(QPalette::Background,QBrush(QColor(100,149,237)));
label->setPalette(palette);
label->setAutoFillBackground(true);
button = new QPushButton(tr("press"),this);
label->move(100,0);
connect(button,SIGNAL(clicked()),this,SLOT(showmy()));
}
MainWindow::~MainWindow()
{
}
void MainWindow::getname(QString str)
{
label->setText(str);
qDebug("getname");
}
void MainWindow::showmy(void)
{
mytest *myt;
myt = new mytest(this);
myt->show();
qDebug("hello");
}
mytest.cpp
#include "mytest.h"
#include "mainwindow.h"
mytest::mytest(QWidget *parent) :
QWidget(parent)
{
this->move(20,20);
this->resize(100,100);
string = "hello";
button = new QPushButton(tr("press"),this);
qDebug("hello");
connect(button,SIGNAL(clicked()),this,SLOT(setstring()));
}
void mytest::setstring()
{
MainWindow mymain;
mymain.getname(string);
qDebug("setstring");
}