Connecting QPushButton to QListWidget
-
Hi All,
Platform: Linux Kubuntu 14
QT version: 4.7.1I 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:
connect(open_element_button, SIGNAL(clicked(QModelIndex)),this,SLOT(onOpenElementButtonPushed(QModelIndex)));
Where open_element_button is
QPushButton
but it didn't work.
I'm pretty sure the SIGNAL(clicked(QModelIndex)) part isn't accurate.Any suggestions ?
Thanks! -
@walle19 said in Connecting QPushButton to QListWidget:
Any suggestions ?
Yes: start by changing over to new style signals & slots before you do anything else.
-
Hi,
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 aQPushButton
that will perform the functionality of double clicking an item in theQListWidget
. -
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 usingQAbstractItemView::doubleClicked(const QModelIndex & index)
.
I looked for a way to get theQModelIndex
in myQPushButton
SLOT().
So initially I thought I will callQListWidget::currentItem()
and thenQListWidget::indexFromItem(QListWidgetItem * item)
.
This would be ideal but that last method is protected and I cannot call it.Any idea ?
-
@walle19 said in Connecting QPushButton to QListWidget:
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 theQModelIndex
of theQListWidgetItem
? -
@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? -
@Christian-Ehrlicher Hi,
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.