PDA

View Full Version : Connecting signals and slots help pls



bod
1st July 2008, 09:40
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:

glWidget = new GLWidget;
tree = new QTreeWidget;
tree->setHeaderLabel("Items");
qtItem = new QTreeWidgetItem(tree);
qtItem->setText(0, tr("QtLogo"));
connect(qtItem , SIGNAL(isSelected()), glWidget, SLOT(setHighlighted()));



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.

mcosta
1st July 2008, 09:49
QTreeWidgetItem isn't QObject child, then you cannot use as SIGNAL sender.

You can connect your slot to QTreeWidget::itemActivated signal

bod
1st July 2008, 13:01
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:

#include <QtGui>

#include "glwidget.h"
#include "window.h"

Window::Window()
{
glWidget = new GLWidget;
tree = new QTreeWidget;
tree->setHeaderLabel("Items");
qtItem = new QTreeWidgetItem(tree);
qtItem->setText(0, tr("QtLogo"));

//tree->addTopLevelItem(tmp);

connect(tree , SIGNAL(itemClicked(qtItem,0)), glWidget, SLOT(setHighlighted()));

QTextBrowser *tb = new QTextBrowser;
QHBoxLayout *mainLayout = new QHBoxLayout;
mainLayout->addWidget(glWidget);
mainLayout->addWidget(tree);
mainLayout->addWidget(tb);
tb->append("asdasda\n");
setLayout(mainLayout);

and here is from my glwidget.cpp file.
My slot function:

void GLWidget::setHighlighted(){

Purple = QColor::fromCmykF(0.25, 0.25, 0.0, 0.0);
qglColor(Purple);
updateGL();
paintGL();
}

mcosta
1st July 2008, 13:13
The correct syntax is



connect(tree , SIGNAL(itemClicked(QTreeWidgetItem*, int)), glWidget, SLOT(setHighlighted()));


In the setHighlighted remove calling to paintGL(): it's automatically called from updateGL()

bod
1st July 2008, 13:48
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.

jpn
1st July 2008, 14:04
Does the class declaration of GLWidget contain required Q_OBJECT macro? Did you declare GLWidget::setHighlighted() as a slot?

bod
1st July 2008, 14:09
Yes, here is my header file:



#ifndef GLWIDGET_H
#define GLWIDGET_H

#include <QGLWidget>
#include "window.h"

class GLWidget : public QGLWidget
{
Q_OBJECT

public:
GLWidget(QWidget *parent = 0);
~GLWidget();

QSize minimumSizeHint() const;
QSize sizeHint() const;

public slots:
void setXRotation(int angle);
void setYRotation(int angle);
void setZRotation(int angle);
void setHighlighted();

signals:
void xRotationChanged(int angle);
void yRotationChanged(int angle);
void zRotationChanged(int angle);

protected:
void initializeGL();
void paintGL();
void resizeGL(int width, int height);
void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);

private:
GLuint makeObject();
void quad(GLdouble x1, GLdouble y1, GLdouble x2, GLdouble y2,
GLdouble x3, GLdouble y3, GLdouble x4, GLdouble y4);
void extrude(GLdouble x1, GLdouble y1, GLdouble x2, GLdouble y2);
void normalizeAngle(int *angle);

GLuint object;
int xRot;
int yRot;
int zRot;
QPoint lastPos;
QColor trolltechGreen;
QColor trolltechPurple;
QColor Purple;
};

#endif


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.

jpn
1st July 2008, 14:13
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 (http://doc.trolltech.com/4.4/debug.html). Notice that QObject::connect() does output a warning in case establishing the connection fails.

bod
1st July 2008, 14:16
So, the mistake is in updating glwidget ??

bod
1st July 2008, 15:01
Okey, I run it debug mode in visual studio.and it enters the function. Thnx for everyone.