Question

I got two windows(two classes), one window that opens another when i click a button.

then the user inputs something into the newly opened window and then transfer that information to the first window when a button is clicked

The problem is I can't seem to send something to the second window so i can send the user input back to the main window. I read a few places that I should use Q_object but not really sure how that works

I should mention that I am new to Qt and didn't know about the designer there is in qt creator before i was way to ahead with the program.

Hope you have some ideas to how I can do this

edit1:

I should show the relevant code i have

Mainwindow.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.cpp

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();
}
}

If that makes sence, If you need the .h file too i can show them also

I need to call setProfileList from the mainwindow in the function saveprofile(there is in createprofile window) or if there is a way i can change the combobox in the wainwindow from the createprofile window?

edit 2:

mainwindow.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
    };

createProfile.h

class CreateProfile : public QMainWindow
{
    Q_OBJECT
public slots:
    void saveProfile();
public:
    explicit CreateProfile(QWidget *parent = 0);
~CreateProfile();
//and other stuff there isnt relevant
};
Was it helpful?

Solution

You are looking for the Qt signal-slot mechanism, namely:

class SecondWindow : public QMainWindow
{
    Q_OBJECT
    public:

    SecondWindow(QWidget *parent = Q_NULLPTR) : QObject(Q_NULLPTR)
    {
        // ...
        connect(secondWindowButton, SIGNAL(clicked(bool)), SLOT(handleClicked()));
        // ...
    }

public slots:
    void SecondWindow::handleClicked()
    {
        // Gather information from the UI as you wish
        firstWindow->foo();
    }
    // ...
}

or, if you have a container class for the windows, you could handle it in there, too, as follows:

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

OTHER TIPS

I found a very different way to do this, so I have made a QComboBox in the constructor of createProfile.cpp and then I have access to the profileList this way.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top