I can't handle doubleClicked signal but clicked work well in using QListView,why?
-
I write code in construction function of my own class which subclass from QListView.
@
connect(this, SIGNAL(clicked(QModelIndex)),this,SLOT(myItemSelected(QModelIndex))); //works properly.
connect(this, SIGNAL(doubleClicked(QModelIndex)),this,SLOT(myItemSelected(QModelIndex)));//doesn't work
@
I just can handle the clicked signal but doubleClicked ,Can anybody tell me why?
My Qt SDK version is 4.7. development enviroment is Win7, Qt CreatorEdit: added @ tags around the code. You can edit your post if needed to correct mistakes; Andre
-
The doubleClicked() signal is triggered for me in the example below. Does the example below reproduce your problem? If not, can you modify it so that it does?
@#include <QtGui>
class ListView : public QListView
{
Q_OBJECTpublic:
ListView()
{
QStringList list;
list << "first" << "second" << "third" << "fourth" << "fith" << "sixth";
QStringListModel *myModel = new QStringListModel(this);myModel->setStringList(list);
setModel(myModel);
connect(this, SIGNAL(clicked(QModelIndex)),this,SLOT(myItemSelected(QModelIndex)));
connect(this, SIGNAL(doubleClicked(QModelIndex)),this,SLOT(doubleClickedSlot(QModelIndex)));
}
public slots:
void myItemSelected(QModelIndex)
{
qDebug() << "Clicked was triggered";
}void doubleClickedSlot(QModelIndex)
{
qDebug() << "Double clicked was triggered";
}
};
#include "main.moc"int main(int argc, char** argv)
{
QApplication app(argc, argv);
ListView view;
view.show();
return app.exec();}@
-
I see no reason at all why it wouldn't work, and I have used this signal myself too without any problems. Are you absolutely positive that you did not make some small mistake in the connect statement in your real code?
-
Thank you very much, Andre. your code is really work well . But my code is a little complicated. The class which subclass from QListView, It's object is decorated with QGraphicsProxyWidget to be layout into a QGraphicsWidget object. and then the doubleClicked signal cannot be handled. without the QGraphicsProxyWidget my code could receive the signal too. I am trying to solve it. If you have some advice for this issue , I will be grateful.
-
[quote author="sigrid" date="1305096445"]The doubleClicked() signal is triggered for me in the example below. Does the example below reproduce your problem? If not, can you modify it so that it does? @#include <QtGui> class ListView : public QListView { Q_OBJECT public: ListView() { QStringList list; list << "first" << "second" << "third" << "fourth" << "fith" << "sixth"; QStringListModel myModel = new QStringListModel(this); myModel->setStringList(list); setModel(myModel); connect(this, SIGNAL(clicked(QModelIndex)),this,SLOT(myItemSelected(QModelIndex))); connect(this, SIGNAL(doubleClicked(QModelIndex)),this,SLOT(doubleClickedSlot(QModelIndex))); } public slots: void myItemSelected(QModelIndex) { qDebug() << "Clicked was triggered"; } void doubleClickedSlot(QModelIndex) { qDebug() << "Double clicked was triggered"; } }; #include "main.moc" int main(int argc, char* argv) { QApplication app(argc, argv); ListView view; view.show(); return app.exec(); }@[/quote]
Thank you very much. your code is really work well . But my code is a little complicated. The class which subclass from QListView, It’s object is decorated with QGraphicsProxyWidget to be layout into a QGraphicsWidget object. and then the doubleClicked signal cannot be handled. without the QGraphicsProxyWidget my code could receive the signal too. I am trying to solve it. If you have some advice for this issue , I will be grateful.
. -
QGraphicsProxyWidget is a problematic thing, it seems. It never quite worked properly, and as I gathered, it is considdered to be failed experiment.
What you might considder, is if you could use a QML ListView element in your view instead of a a QWidget based one. That way, you could avoid using QGraphicsProxyWidget and all the problems it brings with it.
-
[quote author="Andre" date="1305100976"]QGraphicsProxyWidget is a problematic thing, it seems. It never quite worked properly, and as I gathered, it is considdered to be failed experiment. What you might considder, is if you could use a QML ListView element in your view instead of a a QWidget based one. That way, you could avoid using QGraphicsProxyWidget and all the problems it brings with it. [/quote]
Alright , I will consider it. I am really grateful for your reply.
-
bq. Thank you very much. your code is really work well . But my code is a little complicated. The class which subclass from QListView, It’s object is decorated with QGraphicsProxyWidget to be layout into a QGraphicsWidget object. and then the doubleClicked signal cannot be handled. without the QGraphicsProxyWidget my code could receive the signal too. I am trying to solve it. If you have some advice for this issue , I will be grateful.
I am a bit confused. Can you let me know whether your QListView is embedded in a QGraphicsProxyWidget, or whether you have QGraphicsProxyWidgets as items in your view?
-
[quote author="sigrid" date="1305111509"]bq. I am a bit confused. Can you let me know whether your QListView is embedded in a QGraphicsProxyWidget, or whether you have QGraphicsProxyWidgets as items in your view? [/quote]
Yes my QListView is embedded in a QGraphicsProxyWidget, and this QGraphicsProxyWidget was added into a QGraphicsWidget.and then QGraphicsWidget is showed in a QGraphicsView. -
I see. The doubleClicked() signal is still triggered for me when following the approach you describe above. Does the example below reproduce your problem? If not, can you modify it so that it does?
@#include <QtGui>
class ListView : public QListView
{
Q_OBJECTpublic:
ListView()
{
QStringList list;
list << "first" << "second" << "third" << "fourth" << "fith" << "sixth";
QStringListModel *myModel = new QStringListModel(this);myModel->setStringList(list);
setModel(myModel);
connect(this, SIGNAL(clicked(QModelIndex)),this,SLOT(myItemSelected(QModelIndex)));
connect(this, SIGNAL(doubleClicked(QModelIndex)),this,SLOT(doubleClickedSlot(QModelIndex)));
}
public slots:
void myItemSelected(QModelIndex)
{
qDebug() << "Clicked was triggered";
}void doubleClickedSlot(QModelIndex)
{
qDebug() << "Double clicked was triggered";
}
};
#include "main.moc"int main(int argc, char** argv)
{
QApplication app(argc, argv);
QGraphicsView view;
QGraphicsScene scene;
ListView *listView = new ListView;
QGraphicsWidget *widget = new QGraphicsWidget();
QGraphicsProxyWidget *proxy = new QGraphicsProxyWidget(widget);
proxy->setWidget(listView);scene.addItem(widget);
view.setScene(&scene);
view.show();
return app.exec();}@
-
Ok , Indeed the problem is mine not the QGraphicsProxyWidget. because I reimplemented the mouseDoubleClickEvent function of QGraphicsView. So when I double clicked the QListView widget. the signal was send to my QGraphicsView not the QListView. As a result, I lost my doubleClicked signal.Here is my sample code. Do you have solution for this situation? I am a new guy to QT.
@
#include <QtGui/QApplication>
#include <QListView>
#include <QGraphicsView>
#include <QStringList>
#include <QStringListModel>
#include <QDebug>
#include <QGraphicsWidget>
#include <QGraphicsProxyWidget>
#include <QGraphicsGridLayout>
#include <QDirModel>
#include <QPushButton>
class ListView : public QListView
{
Q_OBJECT
public:
ListView()
{
QStringList list;
list << "first" << "second" << "third" << "fourth" << "fith" << "sixth";
QStringListModel *myModel = new QStringListModel(this);
myModel->setStringList(list);
//setModel(myModel);
setModel(new QDirModel());
connect(this, SIGNAL(clicked(QModelIndex)),this,SLOT(myItemSelected(QModelIndex)));
connect(this, SIGNAL(doubleClicked(QModelIndex)),this,SLOT(doubleClickedSlot(QModelIndex))); }
public slots:
void myItemSelected(QModelIndex)
{
qDebug() << "Clicked was triggered";
}
void doubleClickedSlot(QModelIndex)
{
qDebug() << "Double clicked was triggered";
}
};
class GraphicsView:public QGraphicsView
{
public:
GraphicsView(QWidget * parent = 0):QGraphicsView(parent)
{} void mouseDoubleClickEvent(QMouseEvent *event) { // do something. return; }
};
#include "debug/main.moc"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);GraphicsView view; QGraphicsScene scene; ListView *listView = new ListView; QGraphicsWidget *widget = new QGraphicsWidget(); QGraphicsProxyWidget *proxy = new QGraphicsProxyWidget(); proxy->setWidget(listView); QGraphicsGridLayout *pLayout = new QGraphicsGridLayout(); pLayout->addItem(proxy,0,0); widget->setLayout(pLayout); scene.addItem(widget); view.setScene(&scene); view.show(); return a.exec();
}
@ -
[quote author="sigrid" date="1305183595"]I see. The doubleClicked() signal is still triggered for me when following the approach you describe above. Does the example below reproduce your problem? If not, can you modify it so that it does?
@
#include <QtGui> class ListView : public QListView
{
Q_OBJECT
public:
ListView()
{
QStringList list;
list << "first" << "second" << "third" << "fourth" << "fith" << "sixth";
QStringListModel *myModel = new QStringListModel(this);
myModel->setStringList(list);
setModel(myModel);
connect(this, SIGNAL(clicked(QModelIndex)),this,SLOT(myItemSelected(QModelIndex)));
connect(this, SIGNAL(doubleClicked(QModelIndex)),this,SLOT(doubleClickedSlot(QModelIndex)));
}public slots:
void myItemSelected(QModelIndex)
{
qDebug() << "Clicked was triggered";
}
void doubleClickedSlot(QModelIndex)
{
qDebug() << "Double clicked was triggered";
}
};#include "main.moc"
int main(int argc, char** argv)
{
QApplication app(argc, argv);
QGraphicsView view;
QGraphicsScene scene;
ListView *listView = new ListView;
QGraphicsWidget *widget = new QGraphicsWidget();
QGraphicsProxyWidget *proxy = new QGraphicsProxyWidget(widget);
proxy->setWidget(listView);
scene.addItem(widget);
view.setScene(&scene);
view.show();
return app.exec();
}
@[/quote]
Here is my code, thank you for your patience
Edit: fixed-up code formatting; Andre
-
You can call the base class before you do your own stuff in the mouseDoubleClickEvent(), something like:
@
void mouseDoubleClickEvent(QMouseEvent *event)
{
QGraphicsView::mouseDoubleClickEvent(event);
// do something.
return;
}
@Does that give you the behavior you need?
-
[quote author="sigrid" date="1305207781"]You can call the base class before you do your own stuff in the mouseDoubleClickEvent(), something like: @ void mouseDoubleClickEvent(QMouseEvent *event) { QGraphicsView::mouseDoubleClickEvent(event); // do something. return; } @ Does that give you the behavior you need?[/quote]
finally, It works. Thank you very much. I just have no idea why I need to call mouseDoubleClickEvent of the base class . Do I neglect something in Qt Helper?Can you recommend some article about this?
-
The view receives input events from the keyboard and mouse, and translates these to scene events. The scene is responsible of propagating the events to the items on the scene, and if you don't call the base class, then the internal code which propagates the event will not be called and hence your signal not triggered, you can find documentation on this here:
-
Calling the base class implementation of a function you reimplement is usually a good idea. Unless you are certain that you completely want to override everything the base class did (or you know it does nothing), you should call the base implementation by default.
-
[quote author="Andre" date="1305271263"]Calling the base class implementation of a function you reimplement is usually a good idea. Unless you are certain that you completely want to override everything the base class did (or you know it does nothing), you should call the base implementation by default. [/quote]
Lots of thanks to both Andred and sigrid.. this forum is really paradis for Qt users