Results 1 to 14 of 14

Thread: Adding own slot to a signal from mainWindow

  1. #1
    Join Date
    Mar 2014
    Posts
    18
    Qt products
    Qt5

    Question Adding own slot to a signal from mainWindow

    Hello,

    I would like to connect a slider I have created in "Design mode" with an own slot I have written into a new class.

    The slider is named g_sld.

    I get the error. "expected primary-expression before '->' token" in the main.cpp
    Google couldnt help me and as I am a C++ beginner I dont unterstand the mistake.
    Can anybody tell me whats wrong?

    Thank you very much :-)

    See the code following:

    Headers:
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5.  
    6. namespace Ui {
    7. class MainWindow;
    8. }
    9.  
    10. class MainWindow : public QMainWindow
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. explicit MainWindow(QWidget *parent = 0);
    16. ~MainWindow();
    17.  
    18. private:
    19. Ui::MainWindow *ui;
    20. };
    21.  
    22. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. #ifndef MYSLOTS_H
    2. #define MYSLOTS_H
    3.  
    4. #include <QObject>
    5.  
    6. class mySlots : public QObject
    7. {
    8. Q_OBJECT
    9. public:
    10. explicit mySlots(QObject *parent = 0);
    11.  
    12. signals:
    13.  
    14.  
    15. public slots:
    16. void writeValue (int);
    17.  
    18. private:
    19. int val;
    20.  
    21. };
    22.  
    23. #endif // MYSLOTS_H
    To copy to clipboard, switch view to plain text mode 

    Sources:
    main.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2. #include <QApplication>
    3. #include "myslots.h"
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QApplication a(argc, argv);
    8. MainWindow w;
    9. w.show();
    10.  
    11. QObject::connect(MainWindow->g_sld, SIGNAL(valueChanged (int)),SLOT(writeValue(int)));
    12.  
    13. return a.exec();
    14. }
    To copy to clipboard, switch view to plain text mode 

    mywindow.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. using namespace std;
    4.  
    5. MainWindow::MainWindow(QWidget *parent) :
    6. QMainWindow(parent),
    7. ui(new Ui::MainWindow)
    8. {
    9. ui->setupUi(this);
    10. }
    11.  
    12. MainWindow::~MainWindow()
    13. {
    14. delete ui;
    15. }
    To copy to clipboard, switch view to plain text mode 

    myslots.cpp
    Qt Code:
    1. #include "myslots.h"
    2. #include <iostream>
    3. using namespace std;
    4.  
    5. mySlots::mySlots(QObject *parent) :
    6. QObject(parent)
    7. {
    8. int val=0;
    9. }
    10.  
    11. void mySlots::writeValue (int x)
    12. {
    13. if (x != val)
    14. {
    15. cout << x << endl;
    16. val = x;
    17. }
    18. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Adding own slot to a signal from mainWindow

    Qt Code:
    1. connect(MyWindow.ui->g_sld,...);
    To copy to clipboard, switch view to plain text mode 
    supposing g_sld is a pointer.

  3. #3
    Join Date
    Mar 2014
    Posts
    18
    Qt products
    Qt5

    Default Re: Adding own slot to a signal from mainWindow

    Thanks,

    actually g_sld is just in the Form "mainwindow.ui" and I am not sure whether g_sld is automatically a pointer or how to set a pointer to this.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Adding own slot to a signal from mainWindow

    The error arises because the first argument to connect is wrong but there are several issues here.
    Qt Code:
    1. QObject::connect(MainWindow->g_sld, SIGNAL(valueChanged (int)),SLOT(writeValue(int)));
    To copy to clipboard, switch view to plain text mode 
    1. The compiler is expecting to see a pointer-to QObject. The identifier MainWindow is the name of a class not an object of that class, you would be closer with w.g_sld (w is not a pointer so not w->g_sld).
    2. The identifier g_sld is associated with a private member variable in the class MainWindow, and that is not accessible outside the class implementation.
    3. The three argument connect() can only be used inside a QObject implementation because the target object is an implied "this".


    Move the connect() into the MainWindow implementation. Usually in the constructor after the setupUi().

  5. #5
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Adding own slot to a signal from mainWindow

    (1) If the UI file was created by Designer then g_sld is a pointer. See the ui_mainwindow.h file.
    (2) I meant MainWindow, MyWindow is a typo.

  6. #6
    Join Date
    Mar 2014
    Posts
    18
    Qt products
    Qt5

    Default Re: Adding own slot to a signal from mainWindow

    Thank you very much.

    To solve the points 1 and 2 I tried the following:
    I hope this is ok.

    in mainwindow.h:
    Qt Code:
    1. class MainWindow : public QMainWindow
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. explicit MainWindow(QWidget *parent = 0);
    7. ~MainWindow();
    8. //g_sld to public
    9. Ui::MainWindow *g_sld;
    10. private:
    11. Ui::MainWindow *ui;
    12. };
    To copy to clipboard, switch view to plain text mode 

    and in mainwindow.cpp:
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include "myslots.h"
    4. using namespace std;
    5.  
    6. MainWindow::MainWindow(QWidget *parent) :
    7. QMainWindow(parent),
    8. ui(new Ui::MainWindow)
    9. {
    10. ui->setupUi(this);
    11. QObject::connect(ui->g_sld, SIGNAL(valueChanged (int)),SLOT(mySlots::writeValue(int)));
    12.  
    13.  
    14. }
    15.  
    16. MainWindow::~MainWindow()
    17. {
    18. delete ui;
    19. }
    To copy to clipboard, switch view to plain text mode 

    Now I get a new error:

    Qt Code:
    1. QObject::connect: No such slot MainWindow::writeValue(int) in ../EigenerSlot/mainwindow.cpp:10
    2. QObject::connect: (sender name: 'g_sld')
    3. QObject::connect: (receiver name: 'MainWindow')
    To copy to clipboard, switch view to plain text mode 

    I understand the error, but I dont know how to tell Qt the correct Slot.
    I think this has maybe something to do with the third point I dont understand completely ;-)

  7. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Adding own slot to a signal from mainWindow

    Line 9 in mainwindow.h is unnecessary. The variable g_sld already exists as a member of the Ui::MainWindow class pointed to by ui. It is that variable you access (correctly) as the signal source in your revised connect().

    Your revised three-parameter connect() call can only connect to a slot in the same object. If writevalue() was a slot in the MainWindow class you would be good. To connect to a slot in another object you need the four parameter version where you specify the receiving object. In this case you need to have access to a pointer to the receiving object (you do not at the moment). Where is an instance of MySlots created?

  8. #8
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Adding own slot to a signal from mainWindow

    A blackout on my side. I do apologize.
    Qt Code:
    1. connect(w.ui->g_sld, ...);
    To copy to clipboard, switch view to plain text mode 
    MainWindow is a class name, not an instance name.

    As to your last post, the same. You cannot connect to mySlots::writeValue() because nothing like that exists. mySlots is a class name not an instance name. You need to create an instance of mySlots (prior you connect) and connect to it. Remove the modifications from your last post, create an instance of mySlots prior MainWindow w; and connect in the MainWindow ctor (ui is private). If you do not want to connect in the ctor then make the pointer to the slider public:
    Qt Code:
    1. class MainWindow : public QMainWindow
    2. {
    3. public :
    4.  
    5. QSlider *g_sld;
    6.  
    7. ...
    8. };
    9.  
    10. MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
    11. {
    12. ui->setupUi(this);
    13. g_sld = ui->g_sld;
    14. }
    To copy to clipboard, switch view to plain text mode 
    and connect elsewhere. Connect prior w.show();

  9. #9
    Join Date
    Mar 2014
    Posts
    18
    Qt products
    Qt5

    Default Re: Adding own slot to a signal from mainWindow

    Oh my god.

    This is all so complicated.

    With creating an instance you mean that I have to use my function once without a signal, right?
    So I would use it in main.cpp and just write writeVlaue(0);.

    Acutally Ctors are quiet complicated for me at the moment.
    I havent any idea how to build a constructor connection for my stuff.

    So I would prefer the second solution.
    Using your code
    Qt Code:
    1. public :
    2.  
    3. QSlider *g_sld;
    To copy to clipboard, switch view to plain text mode 

    it tells me: 'QSlider' does not name a type.
    I have tried several writings but nothing helped.
    QObject is included.


    And I sould connect in the main.cpp before w.show(), right?
    If I try that I also get errors.
    Replacing the ui in
    Qt Code:
    1. QObject::connect(ui->g_sld, SIGNAL
    To copy to clipboard, switch view to plain text mode 
    with "MainWindow" or with "w" doesent work.


    Also if I try to use the function writeValue in main.cpp it does not work.
    Qt Code:
    1. mySlots::writeValue(0);
    To copy to clipboard, switch view to plain text mode 
    It tells me: cannot call member function 'void mySlots::writeValue(int)' without object
    I have no idea how to solve this.

    Normally I have programmed in C and now C++ is so much more complicated...


    It would be really nice if you could have a look to the file attached.
    This is the programm shown in this thread.

    I know its always the best to solve the own problems on its own, but at the moment I doubt that I am able to do that.

    Therefore it would be very nice if you could fix my programm that I have a working sample to study how it works correctly.


    Thank you very much for your help :-)
    Attached Files Attached Files

  10. #10
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Adding own slot to a signal from mainWindow

    it tells me: 'QSlider' does not name a type.
    Forward declare it Or include QSlider header file.
    Therefore it would be very nice if you could fix my programm that I have a working sample to study how it works correctly.
    You have plenty of such working samples in your Qt installation.
    Oh my god.

    This is all so complicated.
    If you haven't already, please read this book:
    http://www.smart2help.com/e-books/ti...ne/Frames.html
    without a C++ background you will struggle like that all the time.

  11. #11
    Join Date
    Mar 2014
    Posts
    18
    Qt products
    Qt5

    Default Re: Adding own slot to a signal from mainWindow

    Thanks for your answer.

    Quote Originally Posted by stampede View Post
    Forward declare it Or include QSlider header file.
    The header file works
    I don't know how to declare in manually because I don't know on what QSlider is dependent.

    You have plenty of such working samples in your Qt installation.
    Really??? I have Ubuntu and if I search for QT examples I can't find anything.

    If you haven't already, please read this book:
    http://www.smart2help.com/e-books/ti...ne/Frames.html
    without a C++ background you will struggle like that all the time.
    Thanks for the nice link.
    You are completely right.
    I also have a real book in my language here but also with this book it is quite hard.

    For the moment I give up to solve my problem.
    The connect still delivers only errors.

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Adding own slot to a signal from mainWindow

    Quote Originally Posted by Brixus View Post
    I also have a real book in my language here but also with this book it is quite hard.
    It is not enough to have a book, you have to actually read it and do some exercises for it to help.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  13. #13
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Adding own slot to a signal from mainWindow

    As far as C++ is concerned, I strongly recommend Bjarne Stroustrup, the C++ Programming Language, 4th. ed. Search the 4th edition especially, it is about C++11, the contemporary C++ norm. The preceding editions are also good books on C++ but they do not contain the todays C++. The book is on the web (a PDF file) but I do not know whether it is there legally, so that no link publicized on a well behaving forum Eckel is rather obsolete.

    As to the basic problems. If you want a class, you need to specify it first (usually in a header file). You give the class a name (a class name):
    Qt Code:
    1. class MainWindow : public QWindow
    2. {
    3. blah blah blah;
    4. };
    To copy to clipboard, switch view to plain text mode 
    MainWindow is a class name. So far, there are no beasts of the MainWindow kind around. You need to create some if you want to work with them:
    Qt Code:
    1. MainWindow w;
    To copy to clipboard, switch view to plain text mode 
    w is an instance of the class MainWindow. Now, you can reference w. You can call its methods, for example w.show(); or you can reference its data members, for example w.ui->g_sld; The instance is a real thing somewhere in the memory. You cannot write MainWindow.show() or MainWindow.ui->g_sld because MainWindow is only a tag (a type in C) and not something really existing.

    When you create an instance of a class, the ctor (a constructor) will be called if there is some defined in the class. Before you reach the semicolon in
    Qt Code:
    1. MainWindow w;
    To copy to clipboard, switch view to plain text mode 
    the ctor MainWindow::MainWindow() will be called. If there is no ctor, nothing will be done, only a memory for the instance will be reserved. Similarly, a dtor (a destructor) will be called when the instance is going to be removed (if it is going out of scope, if you are calling delete, or on return from a procedure).

    In the ctor, you should do initializing of the instance. Clearing data members, setting pointers to something sensible and, in Qt, connecting the instance so that the instance can send or receive signals. You can do all this later of course but the ctor is a natural place where to initialize.

    Note also, that the statements in your code are executed in the order you have written them. Therefore, if you want to connect a slider in a window class then you need:
    (1) Create the window instance.
    (2) Then create the slider instance, which is a child of the window. The window needs to exist if it should get any childs. In fact, the UI file will create the slider for you in ui->setupUi(this);
    (3) Now, you connect the slider instance (ui->g_sld) to the window.

  14. #14
    Join Date
    Mar 2014
    Posts
    18
    Qt products
    Qt5

    Default Re: Adding own slot to a signal from mainWindow

    Quote Originally Posted by wysota View Post
    It is not enough to have a book, you have to actually read it and do some exercises for it to help.
    You are right.

    I will give my best ;-)

Similar Threads

  1. Replies: 11
    Last Post: 6th October 2013, 19:40
  2. signal slot to mainwindow
    By giugio in forum Newbie
    Replies: 3
    Last Post: 9th November 2012, 20:05
  3. Replies: 8
    Last Post: 7th November 2012, 14:10
  4. Adding icons on title bar of mainwindow
    By Charvi in forum Qt Programming
    Replies: 2
    Last Post: 7th July 2012, 08:23
  5. error when adding signal slot to a QGLWidget based class
    By john_god in forum Qt Programming
    Replies: 2
    Last Post: 11th April 2009, 23: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.