Results 1 to 8 of 8

Thread: Separate Slot Connection in another File

  1. #1
    Join Date
    Jun 2018
    Posts
    12
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Separate Slot Connection in another File

    I made a big dialog window using designer. Can I separate the slots and connections of the windows in another file. I got stuck at the function
    QObject::connect(ui->pushButton,SIGNAL(clicked(bool)),this,SLOT(quit() )) where the third argument give an error.
    I also tried to give a QWidget* to the main window but it doesn't work either.

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Separate Slot Connection in another File

    The signal and slot must have the same argument. You can not connect the signal with the argument to the slot with no arguments.

  3. #3
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    503
    Thanks
    11
    Thanked 76 Times in 74 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Separate Slot Connection in another File

    @Lesiok
    I don't think that's true. You can have less arguments in the slot than in the signal. The other way round is impossible.

    @superpomax:
    "gives an error" is not helpful. What error?

    Ginsengelf

  4. #4
    Join Date
    Jun 2018
    Posts
    12
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Separate Slot Connection in another File

    At first, I went general because it is a big project. Here's the code, I've tried to clean it to only keep the function necessary.
    Also, I wrote some of the code in french, I don't think it is necessary to get what everything means, but I can translate if you need me to.

    On line 10 and 11 of connexion.cpp, I get this error : path\Connexion.cpp:10: error: no matching function for call to 'QObject::connect(QPushButton*&, const char*, Ui::Dialog*&, const char*)'
    QObject::connect(ui->pushButtonChoisirFichierOrigine,SIGNAL(clicked(bo ol)),ui,SLOT(definePathFichierOrigine()));

    Dialog.h
    Qt Code:
    1. #ifndef DIALOG_H
    2. #define DIALOG_H
    3.  
    4. #include <QDialog>
    5. #include "ui_dialog.h"
    6.  
    7. #include "Connexion.h"
    8.  
    9. namespace Ui {
    10. class Dialog;
    11. }
    12.  
    13. class Dialog : public QDialog
    14. {
    15. Q_OBJECT
    16.  
    17. public:
    18. explicit Dialog(QWidget *parent = 0);
    19. ~Dialog();
    20. void createConnexion();
    21. public slots:
    22. void definePathExeConsole();
    23. void definePathFichierOrigine();
    24. public:
    25. Ui::Dialog *ui;
    To copy to clipboard, switch view to plain text mode 
    Dialog.cpp
    Qt Code:
    1. #include "Dialog.h"
    2.  
    3. Dialog::Dialog(QWidget *parent) :
    4. QDialog(parent),
    5. ui(new Ui::Dialog)
    6. {
    7. ui->setupUi(this);
    8.  
    9. createConnexion();
    10. }
    11.  
    12. void Dialog::createConnexion()
    13. {
    14. Connexion connecter(ui);
    15. connecter.connexionSignauxSlots();
    16. }
    17.  
    18. void Dialog::definePathFichierOrigine()
    19. {
    20. ui->labelChoisirFichierOrigine->setText(QFileDialog::getExistingDirectory(this));
    21. }
    22.  
    23. void Dialog::definePathExeConsole()
    24. {
    25. ui->labelChoisirExeConsole->setText(QFileDialog::getOpenFileName(this, "Ouvrir un fichier", QString(), "Exécutable (*.exe)"));
    26. }
    To copy to clipboard, switch view to plain text mode 


    Connexion.h
    Qt Code:
    1. #ifndef CONNEXION_H
    2. #define CONNEXION_H
    3.  
    4. #include "Dialog.h"
    5.  
    6. class Connexion
    7. {
    8. public:
    9. Connexion(Ui::Dialog *dialog){ui = dialog; }
    10. void connexionSignauxSlots();
    11. private:
    12. void connexionSimulation();
    13. private:
    14. Ui::Dialog *ui;
    15. };
    16.  
    17. #endif // CONNEXION_H
    To copy to clipboard, switch view to plain text mode 

    Connexion.cpp
    Qt Code:
    1. #include "Connexion.h"
    2.  
    3. void Connexion::connexionSignauxSlots()
    4. {
    5. connexionSimulation();
    6. }
    7.  
    8. void Connexion::connexionSimulation()
    9. {
    10. QObject::connect(ui->pushButtonChoisirFichierOrigine,SIGNAL(clicked(bool)),ui,SLOT(definePathFichierOrigine()));
    11. QObject::connect(ui->pushButtonChoisirExeConsole,SIGNAL(clicked(bool)),this,SLOT(definePathExeConsole()));
    To copy to clipboard, switch view to plain text mode 
    Last edited by superpomax; 3rd July 2018 at 22:45.

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Separate Slot Connection in another File

    Qt Code:
    1. QObject::connect(ui->pushButtonChoisirFichierOrigine,SIGNAL(clicked(bool)),ui,SLOT(definePathFichierOrigine()));
    To copy to clipboard, switch view to plain text mode 

    In this line, definePathFichierOrigine() is a member function (slot) of the Dialog class, not the Ui:: Dialog class, so that's an error.

    Qt Code:
    1. QObject::connect(ui->pushButtonChoisirExeConsole,SIGNAL(clicked(bool)),this,SLOT(definePathExeConsole()));
    To copy to clipboard, switch view to plain text mode 

    In this line, "this" is an instance of the Connexion class. Not only does it not have a slot named definePathExeConsole(), it isn't derived from QObject at all so it can't have slots.

    Even if you do manage to get pointers to the right class in that function, in my opinion it's a really bad design. You have two classes that must know the intimate details of your GUI - Dialog (so it can create it), and Connexion (so it can do nothing more than connect the signals and slots).

    The whole idea of good C++ design is to encapsulate the details inside a single class so other code that uses that class can simply use the methods of that class and not have to care about how those methods work. Your design makes that impossible - Connexion -must- know about every detail of Ui:: Dialog, right down to the names you choose for the ui widgets, and if you change anything about the UI, you have to at least change Connexion and probably Dialog as well, and rebuild them both.

    It is a guaranteed way to introduce bugs into your code that take a long time to track down.
    Last edited by d_stranz; 4th July 2018 at 01:52.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  6. #6
    Join Date
    Jun 2018
    Posts
    12
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Separate Slot Connection in another File

    For the first one, I declared Dialog a member of the namespace Ui ( line 9 to 11). Why Ui::Dialog and Dialog aren't the same ?
    With your explanation I get why the second line won't work.

    I also get the same error when I try to run my program using Dialog instead of Ui::Dialog.
    Finally, my dialog.cpp file is already at 715 line of code, so I wanted to separate the connections in another file.

    EDIT : I removed Ui and I get class 'Dialog' has no member named 'pushButtonFichierOrigine'. I added another private attribute which Dialog which I give to the constructor of Connexion.
    I can connect signal to slot with a line of code similar to this
    Qt Code:
    1. QObject::connect(window->comboBoxChampForceMOFH2,SIGNAL(currentIndexChanged(int)),ui,SLOT(champForceMOFIdentique()));
    To copy to clipboard, switch view to plain text mode 
    where ui is an instance of Dialog and window is an instance of Ui::Dialog although I don't feel like this is the correct solution t to make the code more modular.
    Last edited by superpomax; 4th July 2018 at 15:45.

  7. #7
    Join Date
    Jun 2018
    Posts
    12
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Separate Slot Connection in another File

    Here's the constructor of Connexion to make it clearer
    Qt Code:
    1. class Connexion
    2. {
    3. public:
    4. Connexion(Dialog *dia,Ui::Dialog *win){ui = dia; window = win; }
    5. private:
    6. void connexionSimulation();
    7.  
    8. private:
    9. Dialog *ui;
    10. Ui::Dialog *window;
    11. };
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Separate Slot Connection in another File

    Why Ui:: Dialog and Dialog aren't the same ?
    Ui:: Dialog is a class named "Dialog" that is created in the "Ui" namespace when your Dialog.ui file is processed by Qt's uic compiler as part of the build procedure. You will see it in your GeneratedFiles folder. "Dialog" is a different class; it is the one you have written that uses the widgets defined as variables in the Ui:: Dialog class.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  9. The following user says thank you to d_stranz for this useful post:

    superpomax (9th July 2018)

Similar Threads

  1. Replies: 3
    Last Post: 9th December 2012, 21:34
  2. Problem connection Signal and slot
    By tamnv110 in forum Newbie
    Replies: 2
    Last Post: 23rd June 2011, 12:36
  3. Signal/Slot connection failing
    By Polnareff in forum Qt Programming
    Replies: 3
    Last Post: 15th July 2010, 21:41
  4. Signal and Slot connection not working !!
    By prakash437 in forum Qt Programming
    Replies: 3
    Last Post: 17th May 2010, 11:16
  5. signal slot connection
    By Big Duck in forum Newbie
    Replies: 2
    Last Post: 4th July 2006, 14:31

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.