Qt: Connect Signals and Slots Across Differnet Files



  • Hello everyone,

    I am a newbie in Qt and C++ programming. I have some problem in my program that i need a solution for.

    I have two files MainWindow.h and ChatWindow.h, which contains two classes of MainWindow and ChatWindow.

    This is chatwindow.h

    @    namespace Ui {
    class ChatWindow;
    }
    
    class ChatWindow : public QMainWindow
    {
        Q_OBJECT
        
    public:
        explicit ChatWindow(QWidget *parent=0);
        ~ChatWindow();
    
    private slots:
        void send_chat_fn(pjsua_call_id call_id);
        void rcv_chat_fn(pjsua_call_id call_id);
        void rcv_msg_fn(QString msg);
    
        void on_pushButton_clicked();
        void on_actionQuit_triggered();
    
    private:
        Ui::ChatWindow *ui;
    };
    

    @
    And this is mainwindow.h

    @ class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
        void add_item(QString buddy_uri);
    
    signals:
        void send_chat(pjsua_call_id call_id);
        void rcv_chat(pjsua_call_id call_id);
        void rcv_msg(QString msg);
    
    private slots:
        void on_actionAdd_Buddy_triggered();
        void on_actionQuit_triggered();
        void on_actionStatus_triggered();
        void on_actionNew_Chat_triggered();
        void on_action_Configuration_triggered();
    
    
        void on_listWidget_doubleClicked(const QModelIndex &index);
    
    private:
        Ui::MainWindow *ui;
    };
    

    @

    Now i want to connect signals from mainwindow.h to slots in chatwindow.h.

    I have tried connection in the constructor of class ChatWindow, but it does not work (i think that is because connections work on instances not on classes). Instance of MainWindow class which i want to connect is in mainwindow.cpp. Defining instances of class ChatWindow in MainWindow gives error:

    bq. > Cannot set parent, parent is in different thread

    And if i create a new instance in Constructor of ChatWindow, then it doesnt connect to the desired instance.

    Its a complete mess. Please help me through this.



  • What you're trying to do is not possible, not with the signal and slot mechanism anyway. ChatWindow and MainWindow are like you said Classes and not instances. The signal and slots mechanism only work on instances.
    What are you trying to make?



  • You are doing something wrong.... this 2 instances can't be in different threads because they are GUI classes and GUI classes in Qt could run only in the main loop...

    How do you call your ChatWindow instance? Are you doing it from MainWindow class?
    If you create ChatWindow in MainWindow class then you should do something like this:
    @
    //this - is MainWindow
    ChatWindow * chat = new ChatWindow(this);
    connect(chat, SIGNAL(send_chat(pjsua_call_id)), this, SLOT(rcv_chat_fn(pjsua_call_id)));
    @

    but if you connect booth instances outside, then make sure what the slots in MainWindow is public and not private...



  • Guessing from the names of your classes I would deduct that the MainWindow class creates the ChatWindow class. So when you create the instance of the ChatWindow class in your instance of MainWindow class you should have pointers for both classes and should be able to establish the signal slot connections.

    Or are both instances of those classes running in separete executables ? Then you would need some sort of interprocess communication (like TCP/IP, CORBA, WebServices, you name it).


  • Moderators

    [quote author="AcerExtensa" date="1370588448"]but if you connect booth instances outside, then make sure what the slots in MainWindow is public and not private...[/quote]
    just for clarification: It's not a problem connecting signals with private, protected and public slots, since the call happens by the QMetaObject which is part of the class itself and thus have the same access rights like the class.


Log in to reply
 

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