Results 1 to 10 of 10

Thread: Connecting signals and slots help pls

  1. #1
    Join Date
    Jun 2008
    Posts
    32
    Qt products
    Qt4

    Default Connecting signals and slots help pls

    Hello,
    I have a object on the left and a treewidget on the right.When I selected an item from the right, I want object to change its color.
    Here is my code:
    Qt Code:
    1. glWidget = new GLWidget;
    2. tree = new QTreeWidget;
    3. tree->setHeaderLabel("Items");
    4. qtItem = new QTreeWidgetItem(tree);
    5. qtItem->setText(0, tr("QtLogo"));
    6. connect(qtItem , SIGNAL(isSelected()), glWidget, SLOT(setHighlighted()));
    To copy to clipboard, switch view to plain text mode 

    However it gives me an error:

    "\Window.cpp(50) : error C2664: 'bool QObject::connect(const QObject *,const char *,const QObject *,const char *,Qt::ConnectionType)' : cannot convert parameter 1 from 'QTreeWidgetItem' to 'const QObject *'
    1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called"

    I did not understand the mistake, help me pls.

  2. #2
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Connecting signals and slots help pls

    QTreeWidgetItem isn't QObject child, then you cannot use as SIGNAL sender.

    You can connect your slot to QTreeWidget::itemActivated signal
    A camel can go 14 days without drink,
    I can't!!!

  3. #3
    Join Date
    Jun 2008
    Posts
    32
    Qt products
    Qt4

    Default Re: Connecting signals and slots help pls

    I did it but it does not work.
    Where is the mistake, I dont get it. I wonder if it calls the required method, so i put a text browser in the window. But It does not see it from the class that I wrote the function in.
    here is my code:
    window.cpp:
    Qt Code:
    1. #include <QtGui>
    2.  
    3. #include "glwidget.h"
    4. #include "window.h"
    5.  
    6. Window::Window()
    7. {
    8. glWidget = new GLWidget;
    9. tree = new QTreeWidget;
    10. tree->setHeaderLabel("Items");
    11. qtItem = new QTreeWidgetItem(tree);
    12. qtItem->setText(0, tr("QtLogo"));
    13.  
    14. //tree->addTopLevelItem(tmp);
    15.  
    16. connect(tree , SIGNAL(itemClicked(qtItem,0)), glWidget, SLOT(setHighlighted()));
    17.  
    18. QHBoxLayout *mainLayout = new QHBoxLayout;
    19. mainLayout->addWidget(glWidget);
    20. mainLayout->addWidget(tree);
    21. mainLayout->addWidget(tb);
    22. tb->append("asdasda\n");
    23. setLayout(mainLayout);
    To copy to clipboard, switch view to plain text mode 

    and here is from my glwidget.cpp file.
    My slot function:
    Qt Code:
    1. void GLWidget::setHighlighted(){
    2.  
    3. Purple = QColor::fromCmykF(0.25, 0.25, 0.0, 0.0);
    4. qglColor(Purple);
    5. updateGL();
    6. paintGL();
    7. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Connecting signals and slots help pls

    The correct syntax is

    Qt Code:
    1. connect(tree , SIGNAL(itemClicked(QTreeWidgetItem*, int)), glWidget, SLOT(setHighlighted()));
    To copy to clipboard, switch view to plain text mode 

    In the setHighlighted remove calling to paintGL(): it's automatically called from updateGL()
    A camel can go 14 days without drink,
    I can't!!!

  5. #5
    Join Date
    Jun 2008
    Posts
    32
    Qt products
    Qt4

    Default Re: Connecting signals and slots help pls

    Nothing changed??
    When i clicked it does not change the Qt logos color.
    I am not even sure of, when i click it calls the setHighlighted method.

  6. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,518 Times in 1,388 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Connecting signals and slots help pls

    Does the class declaration of GLWidget contain required Q_OBJECT macro? Did you declare GLWidget::setHighlighted() as a slot?
    J-P Nurmi

  7. #7
    Join Date
    Jun 2008
    Posts
    32
    Qt products
    Qt4

    Default Re: Connecting signals and slots help pls

    Yes, here is my header file:
    Qt Code:
    1. #ifndef GLWIDGET_H
    2. #define GLWIDGET_H
    3.  
    4. #include <QGLWidget>
    5. #include "window.h"
    6.  
    7. class GLWidget : public QGLWidget
    8. {
    9. Q_OBJECT
    10.  
    11. public:
    12. GLWidget(QWidget *parent = 0);
    13. ~GLWidget();
    14.  
    15. QSize minimumSizeHint() const;
    16. QSize sizeHint() const;
    17.  
    18. public slots:
    19. void setXRotation(int angle);
    20. void setYRotation(int angle);
    21. void setZRotation(int angle);
    22. void setHighlighted();
    23.  
    24. signals:
    25. void xRotationChanged(int angle);
    26. void yRotationChanged(int angle);
    27. void zRotationChanged(int angle);
    28.  
    29. protected:
    30. void initializeGL();
    31. void paintGL();
    32. void resizeGL(int width, int height);
    33. void mousePressEvent(QMouseEvent *event);
    34. void mouseMoveEvent(QMouseEvent *event);
    35.  
    36. private:
    37. GLuint makeObject();
    38. void quad(GLdouble x1, GLdouble y1, GLdouble x2, GLdouble y2,
    39. GLdouble x3, GLdouble y3, GLdouble x4, GLdouble y4);
    40. void extrude(GLdouble x1, GLdouble y1, GLdouble x2, GLdouble y2);
    41. void normalizeAngle(int *angle);
    42.  
    43. GLuint object;
    44. int xRot;
    45. int yRot;
    46. int zRot;
    47. QPoint lastPos;
    48. QColor trolltechGreen;
    49. QColor trolltechPurple;
    50. QColor Purple;
    51. };
    52.  
    53. #endif
    To copy to clipboard, switch view to plain text mode 

    How can i just print just a string, to see whether it calls the setHighlighted function or not. So we would understand that there is a problem in connection or there is a problem in updating the glwidget.

  8. #8
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,518 Times in 1,388 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Connecting signals and slots help pls

    Quote Originally Posted by bod View Post
    How can i just print just a string, to see whether it calls the setHighlighted function or not. So we would understand that there is a problem in connection or there is a problem in updating the glwidget.
    See Debugging Techniques. Notice that QObject::connect() does output a warning in case establishing the connection fails.
    J-P Nurmi

  9. #9
    Join Date
    Jun 2008
    Posts
    32
    Qt products
    Qt4

    Default Re: Connecting signals and slots help pls

    So, the mistake is in updating glwidget ??

  10. #10
    Join Date
    Jun 2008
    Posts
    32
    Qt products
    Qt4

    Default Re: Connecting signals and slots help pls

    Okey, I run it debug mode in visual studio.and it enters the function. Thnx for everyone.

Similar Threads

  1. Signals and Slots
    By 83.manish in forum Qt Programming
    Replies: 3
    Last Post: 30th June 2008, 11:31
  2. Problem with SpinBox signals and slots
    By ramstormrage in forum Newbie
    Replies: 4
    Last Post: 2nd May 2008, 02:45
  3. signals and slots in plugins
    By anderl in forum Qt Programming
    Replies: 1
    Last Post: 10th October 2007, 14:57
  4. Signals and Slots question
    By Thoosle in forum Qt Programming
    Replies: 5
    Last Post: 5th December 2006, 01:24
  5. Connecting signals & slots across different threads
    By jyoti kumar in forum Qt Programming
    Replies: 4
    Last Post: 16th May 2006, 13:40

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.