Results 1 to 5 of 5

Thread: QLIstWidget Problem.

  1. #1
    Join Date
    Jul 2011
    Posts
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default QLIstWidget Problem.

    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.).
    Qt Code:
    1. # include <QApplication>
    2. # include <QListWidget>
    3. # include <QListWidgetItem>
    4. # include <QHBoxLayout>
    5. # include <QDialog>
    6. # include <QMessageBox>
    7.  
    8. class TestList : public QDialog
    9. {
    10. public:
    11.  
    12. TestList()
    13. {
    14. QListWidget *mList = new QListWidget();
    15. mList->addItem("Walmart");
    16. mList->addItem("Ford Motors");
    17. mList->addItem("Circuit City");
    18.  
    19. h->addWidget(mList);
    20. this->setLayout(h);
    21. QObject::connect(mList,SIGNAL(itemDoubleClicked(QListWidgetItem *mItem)),this,SLOT(test(QListWidgetItem *m)));
    22.  
    23.  
    24.  
    25. }
    26.  
    27. public slots:
    28. void test(QListWidgetItem *m)
    29. {
    30.  
    31. QMessageBox *t = new QMessageBox(this);
    32. t->setText(m->text());
    33. t->setVisible(true); // I tried t->show() here as well but to no effect.
    34.  
    35. }
    36. };
    37.  
    38. int main(int argc, char*argv[])
    39. {
    40. QApplication app (argc,argv);
    41. TestList *t = new TestList();
    42. t->show();
    43. return app.exec();
    44. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by coding_neo; 25th July 2011 at 11:38.

  2. #2
    Join Date
    May 2011
    Posts
    239
    Qt products
    Qt4
    Platforms
    Unix/X11 Symbian S60
    Thanks
    4
    Thanked 35 Times in 35 Posts

    Default Re: QLIstWidget Problem.

    What seems to be missing is t->exec(); to execute the message box.

  3. #3
    Join Date
    Apr 2011
    Location
    Russia
    Posts
    85
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    2
    Thanked 13 Times in 13 Posts

    Default Re: QLIstWidget Problem.

    main.cpp:

    Qt Code:
    1. #include <QApplication>
    2. #include <QtGui>
    3. #include "testlist.h"
    4.  
    5. int main(int argc, char*argv[])
    6. {
    7. QApplication app (argc,argv);
    8. TestList *t = new TestList();
    9. t->show();
    10. return app.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 

    testlist.h:

    Qt Code:
    1. #ifndef TESTLIST_H
    2. #define TESTLIST_H
    3.  
    4. #include <QListWidget>
    5. #include <QListWidgetItem>
    6. #include <QHBoxLayout>
    7. #include <QDialog>
    8. #include <QMessageBox>
    9.  
    10. class TestList : public QDialog
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. TestList( QWidget *parent = 0 ):
    16. QDialog( parent )
    17. {
    18. QListWidget *mList = new QListWidget(this);
    19. mList->addItem("Walmart");
    20. mList->addItem("Ford Motors");
    21. mList->addItem("Circuit City");
    22.  
    23. h->addWidget( mList );
    24. setLayout(h);
    25. connect( mList, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(test(QListWidgetItem*)));
    26. }
    27.  
    28. public slots:
    29. void test( QListWidgetItem *m )
    30. {
    31. QMessageBox *t = new QMessageBox( this );
    32. t->setText (m->text() );
    33. t->exec();
    34. }
    35. };
    36.  
    37. #endif // TESTLIST_H
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Nov 2010
    Posts
    20
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    1
    Thanked 4 Times in 4 Posts

    Default Re: QLIstWidget Problem.

    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.

  5. #5
    Join Date
    Jul 2011
    Posts
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QLIstWidget Problem.

    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!

Similar Threads

  1. Problem while using QListWidget
    By deck99 in forum Qt Programming
    Replies: 1
    Last Post: 14th March 2011, 19:29
  2. Problem with QListWidget
    By Elwe in forum Qt Programming
    Replies: 10
    Last Post: 17th June 2009, 21:04
  3. Problem with QListWidget and paintEvent()
    By joksi in forum Qt Programming
    Replies: 2
    Last Post: 29th May 2009, 11:50
  4. QListWidget Problem
    By pmabie in forum Qt Programming
    Replies: 1
    Last Post: 7th October 2007, 07:52
  5. QListWidget-problem
    By Sarma in forum Qt Programming
    Replies: 7
    Last Post: 7th April 2006, 19:49

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.