PDA

View Full Version : QLIstWidget Problem.



coding_neo
25th July 2011, 10:25
Hello:
I have a problem using QListWidget. WHat functionality I would like to have is that I create a QLIstWidget. and I want to be able to doubleclick on an item of the QListWidget and have another UI popup. ( for now I am considering QMessageBox as the other UI).
My code is posted below. When I run it, the QListWidget shows up fine, but when I double click on an item nothing happens ( no message box comes up).
What am I doing wrong? (this code is complete as a standalone, so you can copy and paste it as it is in a file in your ide and run it to get understanding.).


# include <QApplication>
# include <QListWidget>
# include <QListWidgetItem>
# include <QHBoxLayout>
# include <QDialog>
# include <QMessageBox>

class TestList : public QDialog
{
public:

TestList()
{
QListWidget *mList = new QListWidget();
mList->addItem("Walmart");
mList->addItem("Ford Motors");
mList->addItem("Circuit City");

QHBoxLayout *h = new QHBoxLayout();
h->addWidget(mList);
this->setLayout(h);
QListWidgetItem *mItem = new QListWidgetItem();
QObject::connect(mList,SIGNAL(itemDoubleClicked(QL istWidgetItem *mItem)),this,SLOT(test(QListWidgetItem *m)));



}

public slots:
void test(QListWidgetItem *m)
{

QMessageBox *t = new QMessageBox(this);
t->setText(m->text());
t->setVisible(true); // I tried t->show() here as well but to no effect.

}
};

int main(int argc, char*argv[])
{
QApplication app (argc,argv);
TestList *t = new TestList();
t->show();
return app.exec();
}

mvuori
25th July 2011, 11:41
What seems to be missing is t->exec(); to execute the message box.

Jonny174
25th July 2011, 11:46
main.cpp:



#include <QApplication>
#include <QtGui>
#include "testlist.h"

int main(int argc, char*argv[])
{
QApplication app (argc,argv);
TestList *t = new TestList();
t->show();
return app.exec();
}


testlist.h:



#ifndef TESTLIST_H
#define TESTLIST_H

#include <QListWidget>
#include <QListWidgetItem>
#include <QHBoxLayout>
#include <QDialog>
#include <QMessageBox>

class TestList : public QDialog
{
Q_OBJECT

public:
TestList( QWidget *parent = 0 ):
QDialog( parent )
{
QListWidget *mList = new QListWidget(this);
mList->addItem("Walmart");
mList->addItem("Ford Motors");
mList->addItem("Circuit City");

QHBoxLayout *h = new QHBoxLayout();
h->addWidget( mList );
setLayout(h);
connect( mList, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(test(QListWidgetItem*)));
}

public slots:
void test( QListWidgetItem *m )
{
QMessageBox *t = new QMessageBox( this );
t->setText (m->text() );
t->exec();
}
};

#endif // TESTLIST_H

Octal
25th July 2011, 12:34
The code above shows two things :

1/ You forgot the Q_OBJECT macro for signals and slots
2/ You put parameters name instead of just parameters type in SIGNAL and SLOT macros.

By fixing it, your code should work fine.

coding_neo
26th July 2011, 10:33
mvuori , Jonny174, Octal:

Thank you all for your replies. I implemented the advisories you had mentioned and now the code is working as expected.
Thank you very Much!