Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
Connecting QPushButton to QListWidget
walle19 last edited by walle19
Platform: Linux Kubuntu 14
QT version: 4.7.1
I have a QListWidget and want to perform the following:
After pressing (single click) on an element on the list (it will be highlighted) I want to press on a QPushButton that is connected to it via signal slot mechanism, and the slot will perform a specific task on the selected element.
I tried achieving this with the following:
Where open_element_button is
but it didn't work.
I'm pretty sure the SIGNAL(clicked(QModelIndex)) part isn't accurate.
Any suggestions ?
JonB last edited by
Any suggestions ?
Yes: start by changing over to new style signals & slots before you do anything else.
Clicked has no such parameter so you can stop that. Retrieve the selected item in the slot connected to the clicked signal.
Other suggestion: urgently update your distribution and Qt version. Qt 4.7.1 is way more than outdated. If you are stuck to Qt 4 then at least update to the last release of the series (which has also reached end of life a long time ago).
@SGaist Thanks for the reply.
After retrieving the selected item, is there a way to emit a signal that it has been double clicked ?
My goal is to provide a
QPushButtonthat will perform the functionality of double clicking an item in the
Well, you should rather have a slot connected to the QListWidget::itemDoubleClicked signal and then call the same function from the slot connect to your QPushButton::clicked signal.
@SGaist Yes that was what I was going for.
Now consider I am using
QAbstractItemView::doubleClicked(const QModelIndex & index).
I looked for a way to get the
So initially I thought I will call
QListWidget::indexFromItem(QListWidgetItem * item).
This would be ideal but that last method is protected and I cannot call it.
Any idea ?
Any idea ?
QListWidget has a signal QListWidget::itemDoubleClicked(QListWidgetItem*).
@Christian-Ehrlicher Thanks for your reply. This method is good,
But Is there a way to retrieve the
eyllanesc last edited by
@eyllanesc isn't this a protected method ?
I am getting a compile error when compiling it.
......./Qt-4.7.1/include/QtGui/qlistwidget.h:287:17: error: ‘QModelIndex QListWidget::indexFromItem(QListWidgetItem*) const’ is protected QModelIndex indexFromItem(QListWidgetItem *item) const
Why do you need the index at all? Either use a QListWidget and work only with QListWidgetItems or use a QListView and use an own model but do not mix both.
Also why are you using Qt4?
Using QT4 is a constraint.
I need to use index as the chosen row number for a already defined SLOT() that gets a QModelIndex as argument when doubleClicked(QModelIndex) SIGNAL() is sent.
For now I use a workaround - In the QPushButton SLOT() I pass the QAbstractItem::doubleClicked(QModelIndex) SLOT() a non valid QModelIndex.
I check the validity - if valid I use index.row(), if not I call QListWidget::currentRow().
Thanks for all the help
Then use the other signal from the QListWidget which gives you a QListWidgetItem. As I said - mixing them is cumbersome - either use the convenience model or an own but don't mix it.