Qt method call from another class?



  • 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

    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);
    cProfile->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?



  • Ok with some help i found out that

    @cProfile = new CreateProfile;@
    should be
    @cProfile = new CreateProfile(this);@

    and then i should be able to do

    @parent()->setProfileList(temp);@

    but this is not working i get this error

    bq. error: C2039: 'setProfileList' : is not a member of 'QObject'


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    This question has been asked many times lately, please search the forum.

    Anyway, for such a functionality, the common way is to use Signals & Slots. I would recommend that you take a look at Qt's documentation and examples has they show how to do it very well and will give you the basics to go on with your project


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.