0
votes

I have a small problem. I want run function in MainWindow from AnotherWindow. I can't set connect() for it.

Main class: MainWindow
Other form: AnotherWindow
Function in main class: setVariable(QString)
Function in other form: btnClicked()

I have now connected button signal clicked():

// In AnotherWindow.cpp
connect(ui->btnOK, SIGNAL(clicked()), this, SLOT(btnOkClicked()));

// Function in same file
void interfaceWindow::btnOkClicked() {
    /* Some actions - emit signal? */
    this->close();
}

btnOkClicked() are declared as private slot.

// In MainWindow.cpp
void MainWindow::setVariable(QString _var) {
    this->var = _var;
}

setVariable(QString) are declared as public slot.

How I can send variable from AnotherForm (from btnOkClicked() function) to MainWindow (setVariable(QString) function) ? How and where I must send signal and make connection?

I readed about signals and slots, but my code don't work - I don't paste it here because it's terrible :)

Any help for Qt newbie?

3
what exactly does doesn't work mean, doesn't work as it it doesn't compile, or doesn't work as in I tried to click the button but nothing happensRadu Chivu
I can't compile my program with code from my mind and Google - I don't know how I can do it, too many informations for me.aso

3 Answers

0
votes

I'm not entirely sure I understand your question, but let me try.

You want to be able to fire a slot in another class. There are a few ways you can do that.

  1. Declare one as a friend class to the other. Then they can see the protected and private variables/memebers
  2. It is possible to make slots static so you can call them without a class object.

For example,

class MainWindow {
    private slot:
    void setVariable(QString);
}



class AnotherWindow {    
    friend class MainWindow;    
    MainWindow *window;

public:
    AnotherWindow() {
       connect(this, SIGNAL(fire(QString)), window, SLOT(setVariable(QString)));
    } 

signals:
    void fire(QString);
public slots:
   void onButtonClicked() {
       emit fire(QString);
   }
}

The previous is pseudocode so don't expect it to compile. I think this is what you want. Basically since your slot is private on MainWindow you need to make it a friend. To connect, it needs to be a member. Then when the onButtonClicked slot is evoked, then it fire()s the setVarialbe() slot.

0
votes

Here is a simple code for your another window:

class MyWidget : public QWidget
{
Q_OBJECT
   public:

MyWidget(QWidget * parent = 0)
{
    okBtn = new QPushButton ("I am Ok!");
    MyData = "";
    connect(okBtn ,SIGNAL(clicked()),this,SLOT(OnOk()));

}
~MyWidget();

private:
  QString MyData;
QPushButton * okBtn;
  //something that modify string MyData
 signals:

    void MyDataSignal(QString);
//Internal slot that emits signal with proper data
private slots:
    void OnOk()
    {
        if(MyData!="")
        {
            emit MyDataSignal(MyData);
        }
    }

};

Now in MainWindow create an object of MyWidget (suppose myWid)and connect it to slot

connect(myWid, SIGNAL(MyDataSignal(QString)),this,SLOT(OnMyWidOkClicked(QString)));

the signal will pass string to slot.

While making signals and slots keep in mind following points:

  1. To connect a signal to a slot (or to another signal), they must have the same parameter
  2. Parameters should be in the same order in both signal and slot.
  3. if a signal has more parameters than the slot it is connected to, the additional parameters are simply ignored but opposite is not possible.
  4. If you will connect a signal that have unmatched parameters to slot then no compile time error will occur but at run time command window will show a warning that signal/slot/connection does not exist.
0
votes

You need to have an reference of AnotherWindow in MainWindow OR vice versa. Then you need the following things:

// AnotherWindow.h
signals:
    void buttonOkClickedSignal(QString var);

// AnotherWindow.cpp
void interfaceWindow::btnOkClicked() {
    emit buttonOkClickedSignal("The button got clicked!");
    this->close();
}

Next step varies based on whether MainWindow has reference to AnotherWindow or vice versa. You can either:

// AnotherWindow.cpp
connect(this, SIGNAL(buttonOkClickedSignal(QString), &mainWindow, SLOT(setVariable(QString)));

or:

// MainWindow.cpp
connect(&anotherWindow, SIGNAL(buttonOkClickedSignal(QString), this, (SLOT(setVariable(QString)));

If you are invoking the slot through signal it shouldn't matter whether it's private or public (see Qt Documentation). Hope this helps.