C++ 从另一个类调用Qt方法?

C++ 从另一个类调用Qt方法?,c++,qt,qtgui,qmainwindow,qt-signals,C++,Qt,Qtgui,Qmainwindow,Qt Signals,我有两个窗口(两个类),一个窗口在我点击按钮时打开另一个窗口 然后,用户在新打开的窗口中输入一些内容,然后在单击按钮时将该信息传输到第一个窗口 问题是我似乎无法将某些内容发送到第二个窗口,以便将用户输入发送回主窗口。我读了一些地方,我应该使用Q_对象,但不确定它是如何工作的 我应该提到的是,我是Qt的新手,在我开始使用该程序之前,我不知道Qt creator中的设计师 希望你对我如何做到这一点有一些想法 编辑1: 我应该显示我的相关代码 main window.cpp MainWindow::M

我有两个窗口(两个类),一个窗口在我点击按钮时打开另一个窗口

然后,用户在新打开的窗口中输入一些内容,然后在单击按钮时将该信息传输到第一个窗口

问题是我似乎无法将某些内容发送到第二个窗口,以便将用户输入发送回主窗口。我读了一些地方,我应该使用Q_对象,但不确定它是如何工作的

我应该提到的是,我是Qt的新手,在我开始使用该程序之前,我不知道Qt creator中的设计师

希望你对我如何做到这一点有一些想法

编辑1:

我应该显示我的相关代码

main window.cpp

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent)
{
//alot of stuff not relevant right now creating different stuff
changeProfile= new QPushButton("ændre valgte profil",this);
profileList = new QComboBox(this);
cProfile = new CreateProfile;
connect(changeProfile,SIGNAL(clicked()),this,SLOT(ProfileChange()));
}

void MainWindow::ProfileChange()
{
    file pfile(profileList->currentText().toStdString());
    string tempName = pfile.read(light);

    cProfile->setValue(light,tempName);
    cPr

ofile->show();
}
void MainWindow::setProfileList(QString Pname_)
{
    bool found = 0;
    for (int i = 0;i<5;i++)
    {
        if (Pname_ ==profileList->itemText(i))
            found = 1;
    }
    if (found !=1)
        profileList->addItem(Pname_);
}
CreateProfile::CreateProfile(QWidget *parent)
    :QMainWindow(parent)
{
//alot of other irrelevant stuff here
saveP = new QPushButton("Save",this);
connect(saveP,SIGNAL(clicked()),this,SLOT(saveProfile()));
}
void CreateProfile::saveProfile()
{
temp = pName->text();
file pFile(temp.toStdString());
bool lights[2] = {light1->checkState(),light2->checkState()};
if (temp.length() == 0)
{
    MessageBox(NULL,"Du har ikke skrevet noget navn ind\n Prov igen","Error",MB_ICONWARNING+MB_SETFOREGROUND);
}
else
{
    pFile.save(lights);
    //call function setProfileList
    this->hide();
}
}
如果有意义的话,如果你也需要.h文件,我也可以给你看

我需要在函数saveprofile(createprofile窗口中有)的主窗口中调用
setProfileList
,或者是否有办法从createprofile窗口更改wainwindow中的组合框

编辑2:

main window.h

#include "createprofile.h"
class MainWindow : public QMainWindow
{
    Q_OBJECT
public slots:
void ProfileChange();
//some other button clicks
public:
    CreateProfile *cProfile;
    void setProfileList(QString Pname_);
//other irelevant stuff

    private:
    // and some private members
    };
class CreateProfile : public QMainWindow
{
    Q_OBJECT
public slots:
    void saveProfile();
public:
    explicit CreateProfile(QWidget *parent = 0);
~CreateProfile();
//and other stuff there isnt relevant
};
createProfile.h

#include "createprofile.h"
class MainWindow : public QMainWindow
{
    Q_OBJECT
public slots:
void ProfileChange();
//some other button clicks
public:
    CreateProfile *cProfile;
    void setProfileList(QString Pname_);
//other irelevant stuff

    private:
    // and some private members
    };
class CreateProfile : public QMainWindow
{
    Q_OBJECT
public slots:
    void saveProfile();
public:
    explicit CreateProfile(QWidget *parent = 0);
~CreateProfile();
//and other stuff there isnt relevant
};
您正在寻找的目标是:

或者,如果您有windows的容器类,也可以在其中处理它,如下所示:

connect(secondWindow->myButton, SIGNAL(clicked(bool)), SLOT(handleClicked()));

我发现了一种非常不同的方法来实现这一点,因此我在
createProfile.cpp
的构造函数中创建了一个
QComboBox
,然后我就可以通过这种方法访问配置文件列表了。

Hmm不确定您是否完全理解我试图做的事情(或者我不理解您:))我发布了我的.cpp文件,也许可以帮助清除它up@Sumsar1812:
cProfile=newcreateprofile(此文件)然后是另一个类的
parent()->setProfileList()
。嗯,我之前尝试过,但是现在使用()确实给了我一个错误
error:C2039:'setProfileList':不是'QObject'的成员。
我正在传递一个
QString parent()->setProfileList(temp)@Sumsar1812:您是否尝试将parent()的返回值强制转换为类
mainWindow=qobject_cast(parent())if(mainWindow)mainWindow->setProfileList(temp),但最好是从容器类管理这些窗口,真的。这个答案对我来说没有意义。您想访问setProfileList,它是一种方法。在另一个类中拥有一个数据成员并不能真正帮助实现这一点。在这一点上,我恐怕无法理解你的答案。哦,是的,但正如我之前所说的“或者如果有办法我可以更改组合框…”这个函数所做的只是更改组合框,我认为我无法访问另一个类的成员,所以我最终为我创建了一个函数来执行此操作,希望我能在另一个类中使用这个函数。我明白了,希望这就是森切克。我还不确定你为什么有两个主窗口。听起来很奇怪。你能详细说明一下吗?我只是对Qt(和通用编程)非常陌生,在你告诉我这个之前,我不知道我有两个主窗口。这正是我从第一个窗口知道的方式,所以我认为第二个窗口基本上是一样的:我想你希望有一个主窗口,然后在里面有小部件。