Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Qt method call from another class?

Qt method call from another class?

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 1.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sumsar1812
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      sumsar1812
      wrote on last edited by
      #2

      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'

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        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

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved