Results 1 to 19 of 19

Thread: Signals Slots and Class Pointers

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Signals Slots and Class Pointers

    We'd have to see your actual code as currently with those little pieces you posted it is hard to tell anything. In general you need your method to be declared as a slot and you need to be passing a pointer to an instance of that class to the connect() statement to be able to connect to the slot.
    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.


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

    Default Re: Signals Slots and Class Pointers

    Also look at the debug output of your program for warnings of the type:
    Qt Code:
    1. QObject::connect: No such slot GLWidget::SetValue(int)
    To copy to clipboard, switch view to plain text mode 
    indicating that the SetValue() function is not declared as a slot. The glwidget.h file should look like:
    Qt Code:
    1. ...
    2. class GLWidget: public ...
    3. {
    4. Q_OBJECT
    5. public:
    6. GLWidget(...);
    7. ...
    8. public slots: // <<<< this
    9. void SetValue(int value);
    10. ...
    11. };
    12. ...
    To copy to clipboard, switch view to plain text mode 
    and be included in the PRO file HEADERS variable.
    "We can't solve problems by using the same kind of thinking we used when we created them." -- Einstein
    If you are posting code then please use [code] [/code] tags around it - makes addressing the problem easier.

  3. #3
    Join Date
    Jun 2011
    Posts
    203
    Qt products
    Qt4
    Platforms
    MacOS X Windows
    Thanks
    7
    Thanked 4 Times in 3 Posts

    Default Re: Signals Slots and Class Pointers

    Looks like HEADERS in pro are fine.

    As for the code...

    mainwindow.h

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

    glwidget.h

    Qt Code:
    1. #ifndef GLWIDGET_H
    2. #define GLWIDGET_H
    3.  
    4. #include <QtOpenGL/QGLWidget>
    5. #include <cmath>
    6. #include "mainwindow.h"
    7. #include <QMessageBox>
    8.  
    9. class GLWidget : public QGLWidget
    10. {
    11. Q_OBJECT
    12. public:
    13. explicit GLWidget(QWidget *parent = 0);
    14.  
    15. int m_Angle;
    16.  
    17.  
    18. void initializeGL();
    19. void paintGL();
    20. void resizeGL(int w, int h);
    21.  
    22. public slots:
    23. void SetAngle(int Angle);
    24.  
    25. };
    26.  
    27. #endif // GLWIDGET_H
    To copy to clipboard, switch view to plain text mode 

    mainwindow.cpp

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. MainWindow::MainWindow(QWidget *parent) :
    5. QMainWindow(parent),
    6. ui(new Ui::MainWindow)
    7. {
    8. ui->setupUi(this);
    9. connect(ui->horizontalSlider, SIGNAL(valueChanged(int)), ui->Widget, SLOT(SetAngle(int Angle))); //Widget is just the standard widget which I've promoted to GLWidget in form editor
    10. connect(ui->horizontalSlider, SIGNAL(valueChanged(int)), ui->label, SLOT(setNum(int)));
    11. }
    12.  
    13. MainWindow::~MainWindow()
    14. {
    15. delete ui;
    16. }
    To copy to clipboard, switch view to plain text mode 

    glwidget.cpp

    Qt Code:
    1. #include "glwidget.h"
    2.  
    3. GLWidget::GLWidget(QWidget *parent) :
    4. QGLWidget(parent)
    5. {
    6.  
    7. }
    8.  
    9. void GLWidget::SetAngle(int Angle)
    10. {
    11. if (Angle != m_Angle)
    12. {
    13. m_Angle = Angle;
    14. QMessageBox::information(0, "inside slot", "inside slot");
    15. }
    16. }
    To copy to clipboard, switch view to plain text mode 

    P.S. There might be one or two mistakes here or there e.g. m_Angle used instead of Angle or something like that... I'm 99% sure I don't have that mistake in my actual program.
    Last edited by Atomic_Sheep; 19th July 2012 at 07:32.

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

    Default Re: Signals Slots and Class Pointers

    In the last few posts the slot has been referred to as SetNum(), SetValue(), and now SetAngle(). I asked you to check for run time error messages like:
    Qt Code:
    1. QObject::connect: No such slot GLWidget::SetAngle(int Angle)
    To copy to clipboard, switch view to plain text mode 
    Line 9 of mainwindow.cpp is probably generating one because the slot signature is incorrect. You do not include the argument names in the signal or slot signatures.

  5. #5
    Join Date
    Jun 2011
    Posts
    203
    Qt products
    Qt4
    Platforms
    MacOS X Windows
    Thanks
    7
    Thanked 4 Times in 3 Posts

    Default Re: Signals Slots and Class Pointers

    Forgot to say that yes I did check for those errors, I didn't see any, but I just realised that run-time errors are under "Application Output" and yes it's spitting out the "no such slot" error. i.e. :

    Object::connect: No such slot GLWidget::SetAngle(int Angle) in ...\MainWindow.cpp:9
    Object::connect: (sender name: 'horizontalSlider')
    Object::connect: (receiver name: 'Widget')

    ...exited with code 0

    I used different names for my slots previously because I was simply typing the code up as I was writing up the message. In the latest iteration it was copy paste with some minor editing, but the latest code that I provided should have all the naming issues sorted out.

  6. #6
    Join Date
    Jun 2011
    Posts
    203
    Qt products
    Qt4
    Platforms
    MacOS X Windows
    Thanks
    7
    Thanked 4 Times in 3 Posts

    Default Re: Signals Slots and Class Pointers

    Still unable to get my signals and slots to work .

    my .cpp files are :main.cpp, button.cpp, mainWindow.cpp and mainWindowWidget.cpp.

    mainWindowWidget.cpp has the following signal:

    Qt Code:
    1. void mainWindowWidget::mousePressEvent(QMouseEvent *event)
    2. {
    3. if(event->button() == Qt::LeftButton) //left mouse button - need to check this.
    4. {
    5. cMousePositionAtClick = event->pos();
    6. emit MouseClickLocation(cMousePositionAtClick);
    7. //QMessageBox::information(this, "Mouse Click Detected", "Mouse Click Detected");
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 

    I have a slot in my button.h and its implementation.

    here's the code in main.cpp:

    Qt Code:
    1. mainWindow w;
    2. Button button1(0,0,50,50);
    3. connect(w, SIGNAL(MouseClickLocation(QPoint)), button1, SLOT(LocationGrabber(QPoint)));
    4. w.show();
    To copy to clipboard, switch view to plain text mode 

    However when I compile it I get the following errors:

    main.cpp:11: error: cannot convert 'mainWindow' to 'SOCKET' for argument '1' to 'int connect(SOCKET, const sockaddr*, int)'
    Last edited by Atomic_Sheep; 3rd September 2012 at 06:40.

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

    Default Re: Signals Slots and Class Pointers

    Two basic C++, not Qt, problems:

    The error message tells you that the compiler has found only function 'int connect(SOCKET, const sockaddr*, int)' when trying to compile line 11 (I assume line 3 of your posted snippet). The arguments you have given cannot be coerced into the arguments required by that function so it fails. You want QObject::connect() and you cannot access that as an unqualified name outside of the implementation of a QObject sub-class. Use the static QObject::connect() function.

    QObject::connect() requires a pointer to an object, i.e. &w and &button1, not the actual objects as you are current trying to do.

  8. #8
    Join Date
    Jun 2011
    Posts
    203
    Qt products
    Qt4
    Platforms
    MacOS X Windows
    Thanks
    7
    Thanked 4 Times in 3 Posts

    Default Re: Signals Slots and Class Pointers

    Deleted... figured it out.
    Last edited by Atomic_Sheep; 4th September 2012 at 08:29.

  9. #9
    Join Date
    Jun 2011
    Posts
    203
    Qt products
    Qt4
    Platforms
    MacOS X Windows
    Thanks
    7
    Thanked 4 Times in 3 Posts

    Default Re: Signals Slots and Class Pointers

    Thanks Chris for helping out with the previous problem.

    Another question. In the Qt example with the spinning boxes:

    Qt Code:
    1. Window::Window()
    2. {
    3. QGridLayout *mainLayout = new QGridLayout;
    4.  
    5. for (int i = 0; i < NumRows; ++i) {
    6. for (int j = 0; j < NumColumns; ++j) {
    7. QColor clearColor;
    8. clearColor.setHsv(((i * NumColumns) + j) * 255
    9. / (NumRows * NumColumns - 1),
    10. 255, 63);
    11.  
    12. glWidgets[i][j] = new GLWidget(0, 0);
    13. glWidgets[i][j]->setClearColor(clearColor);
    14. glWidgets[i][j]->rotateBy(+42 * 16, +42 * 16, -21 * 16);
    15. mainLayout->addWidget(glWidgets[i][j], i, j);
    16.  
    17. connect(glWidgets[i][j], SIGNAL(clicked()),
    18. this, SLOT(setCurrentGlWidget()));
    19. }
    20. }
    To copy to clipboard, switch view to plain text mode 

    The code creates glWidget objects for each of the cubes to spin. I just have:

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

    and a .cpp file for mainwindowGLWidget.

    I'm trying to connect a signal from another class to a slot of my mainwindowGLWidget class, however I don't know the name of the object of my mainwindowGLWidget class. How do I find this out?

    My limited understanding is that when the compiler runs, ?moc? generates the ui.h file in the -build-desktop folder? But does this somehow solve my problem?

    I know it says this in relation to my glwidget...

    widget = new MyOpenGLWidget(centralWidget);

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

    Default Re: Signals Slots and Class Pointers

    uic generates the ui_stuff.h file from your stuff.ui file.

    The name of the variable holding a pointer to your custom widget is set by you in Designer and carried over into generated UI code. In Designer:
    untitled.jpg
    and in the generated code is the class that the ui pointer points to an instance of:
    Qt Code:
    1. #include <QtGui/QButtonGroup>
    2. #include <QtGui/QFrame>
    3. #include <QtGui/QHBoxLayout>
    4. #include <QtGui/QHeaderView>
    5. #include <QtGui/QWidget>
    6.  
    7. QT_BEGIN_NAMESPACE
    8.  
    9. class Ui_Form
    10. {
    11. public:
    12. QHBoxLayout *horizontalLayout;
    13. QFrame *myCustomFrame; // <<<<< here
    14.  
    15. void setupUi(QWidget *Form)
    16. {
    17. ...
    18. // <<<< and here
    19. myCustomFrame = new QFrame(Form);
    20. myCustomFrame->setObjectName(QString::fromUtf8("myCustomFrame"));
    21. myCustomFrame->setFrameShape(QFrame::Box);
    22. ...
    23. } // setupUi
    24. ...
    25. };
    26. ...
    27. #endif // UI_UNTITLED_H
    To copy to clipboard, switch view to plain text mode 
    so in your code you can access it through the ui pointer thus:
    Qt Code:
    1. ui->myCustomFrame;
    To copy to clipboard, switch view to plain text mode 

  11. #11
    Join Date
    Jun 2011
    Posts
    203
    Qt products
    Qt4
    Platforms
    MacOS X Windows
    Thanks
    7
    Thanked 4 Times in 3 Posts

    Default Re: Signals Slots and Class Pointers

    Thanks Chris... had to reread this whole thread a few times to pick out to what information I applied the principle of 'selective hearing'. Finally got everything to work as desired! Now comes the fun task of fixing the next problem caused by the solving of the old prob . Thanks again.

Similar Threads

  1. Custom QTreeWidgetItem class and signals&slots issue
    By devla in forum Qt Programming
    Replies: 3
    Last Post: 7th July 2012, 01:15
  2. Replies: 3
    Last Post: 6th April 2012, 17:44
  3. Replies: 8
    Last Post: 15th July 2010, 22:42
  4. Access a class without using Signals/Slots
    By impeteperry in forum Qt Programming
    Replies: 5
    Last Post: 10th January 2010, 12:14
  5. pointers behaviour in qt and specially signals/slot mechanism
    By salmanmanekia in forum Qt Programming
    Replies: 5
    Last Post: 17th August 2008, 20:34

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
  •  
Qt is a trademark of The Qt Company.