Results 1 to 15 of 15

Thread: How to set event click Custom ListView

  1. #1
    Join Date
    Jun 2011
    Posts
    35
    Thanks
    16
    Qt products
    Qt3 Qt4
    Platforms
    Windows Symbian S60

    Default How to set event click Custom ListView

    I want to set event click of the individual item of the row (only 1 column)?

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: How to set event click Custom ListView

    Please be more descriptive, your post is not clear of what you want.

    You want to connect to item click signal?
    or
    You want to receive the event in you view class, when item is clicked?

  3. The following user says thank you to Santosh Reddy for this useful post:

    jindoniit (9th June 2011)

  4. #3
    Join Date
    Jun 2011
    Posts
    35
    Thanks
    16
    Qt products
    Qt3 Qt4
    Platforms
    Windows Symbian S60

    Default Re: How to set event click Custom ListView

    let me describe in more detail
    Example
    I have a custom list and each row has:
    One Icon
    One Labels
    One button
    yes, I want to receive the event in my view class, when Icon is clicked

  5. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: How to set event click Custom ListView

    If your having a custom QListView, you can receive the event in
    Qt Code:
    1. void QListView::mouseReleaseEvent ( QMouseEvent * e )
    To copy to clipboard, switch view to plain text mode 
    in there check over which item mouse was released, and it is over the icon area.

  6. The following user says thank you to Santosh Reddy for this useful post:

    jindoniit (9th June 2011)

  7. #5
    Join Date
    Jun 2011
    Posts
    35
    Thanks
    16
    Qt products
    Qt3 Qt4
    Platforms
    Windows Symbian S60

    Default Re: How to set event click Custom ListView

    i create custom listview as this

    and i write a mouseReleaseEvent but how to assign icon event or function when clicking on it.
    I think you understand what I mean.Please guide how to resolve this problem

  8. #6
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: How to set event click Custom ListView

    are you using QStandardItem or cutom Item ?

  9. The following user says thank you to Santosh Reddy for this useful post:

    jindoniit (9th June 2011)

  10. #7
    Join Date
    Jun 2011
    Posts
    35
    Thanks
    16
    Qt products
    Qt3 Qt4
    Platforms
    Windows Symbian S60

    Default Re: How to set event click Custom ListView

    Qt Code:
    1. item1->setData(tr("ItemView %1").arg(i+1),ListviewDelegate::headerTextRole);
    2. item1->setData("Some examples the use of graphics effects with canvas items.",ListviewDelegate::subHeaderTextrole);
    3. item1->setData(icon,ListviewDelegate::IconRole1);
    4. item1->setData(label,ListviewDelegate::IconRole2);
    5. item1->setEditable(false);
    6. item1->setSelectable(false);
    7. model->appendRow(item1);
    To copy to clipboard, switch view to plain text mode 


    you can see I am using QStandardItem to write Custom listview, in QStandardItem i set data include : icon ,text ,label.
    now , i want set 1 function for icon, 1 finction for label
    Last edited by jindoniit; 9th June 2011 at 09:46.

  11. #8
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: How to set event click Custom ListView

    Have a look at this solution

    1. Use the QListWidget directly
    2. Create a custom QListWidgetItem (say QLWIPushButton)
    3. QLWIPushButton is both a QWidget and QListWidgetItem. I have used QWidget so that I can get mouse events.
    4. QLWIPushButton will have a QLabel, and QPushButton on it (you can add more if you want)
    5. Again one more issue here is, QLabel does not directly support mose clicking (unless used as hyperlink), we have make a custom QLabel (say CustomWidget)

    Qt Code:
    1. //MainWindow.h
    2. class MainWindow : public QMainWindow
    3. {
    4. ...
    5. protected slots:
    6. void buttonClicked(int row);
    7. void labelClicked(int row);
    8. private:
    9. Ui::MainWindow *ui;
    10.  
    11. QListWidget* listWidget;
    12. QLineEdit* rowLineEdit;
    13. QLineEdit* widgetLineEedit;
    14. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. //MainWindow.cpp
    2. MainWindow::MainWindow(QWidget *parent)
    3. : QMainWindow(parent)
    4. , ui(new Ui::MainWindow)
    5. {
    6. ui->setupUi(this);
    7.  
    8. QDockWidget* dock = new QDockWidget("Display Widget");
    9. QWidget* widget = new QWidget(this);
    10. QGridLayout* layout = new QGridLayout(widget);
    11.  
    12. QLabel* row_label = new QLabel("Row", widget);
    13. QLabel* widget_label = new QLabel("Widget", widget);
    14. rowLineEdit = new QLineEdit("Click a row in List Widget", widget);
    15. widgetLineEedit = new QLineEdit("Click a row in List Widget", widget);
    16.  
    17. layout->addWidget(row_label, 0 , 0, 1, 1);
    18. layout->addWidget(rowLineEdit, 0 , 1, 1, 1);
    19. layout->addWidget(widget_label, 1 , 0, 1, 1);
    20. layout->addWidget(widgetLineEedit, 1 , 1, 1, 1);
    21.  
    22. widget->setLayout(layout);
    23. dock->setWidget(widget);
    24. addDockWidget(Qt::RightDockWidgetArea, dock);
    25.  
    26. // Custom QListWidgetItem in QListWidget
    27. listWidget = new QListWidget(this);
    28. listWidget->addItem(new QListWidgetItem(QString("Standard QListWidgetItem 1")));
    29. QLWIPushButton* new_item1 = new QLWIPushButton(QString("Custom QListWidgetItem 1"), listWidget);
    30. listWidget->addItem(new QListWidgetItem(QString("Standard QListWidgetItem 2")));
    31. listWidget->addItem(new QListWidgetItem(QString("Standard QListWidgetItem 3")));
    32. QLWIPushButton* new_item2 = new QLWIPushButton(QString("Custom QListWidgetItem 2"), listWidget);
    33. listWidget->addItem(new QListWidgetItem(QString("Standard QListWidgetItem 4")));
    34.  
    35. listWidget->setAlternatingRowColors(true);
    36.  
    37. // Make itemClicked Connection
    38. connect(new_item1, SIGNAL(buttonClicked(int)), this, SLOT(buttonClicked(int)));
    39. connect(new_item1, SIGNAL(labelClicked(int)), this, SLOT(labelClicked(int)));
    40.  
    41. // Make itemClicked Connection
    42. connect(new_item2, SIGNAL(buttonClicked(int)), this, SLOT(buttonClicked(int)));
    43. connect(new_item2, SIGNAL(labelClicked(int)), this, SLOT(labelClicked(int)));
    44.  
    45. setCentralWidget(listWidget);
    46. }
    47.  
    48. MainWindow::~MainWindow()
    49. {
    50. delete ui;
    51. }
    52.  
    53. void MainWindow::buttonClicked(int row)
    54. {
    55. rowLineEdit->setText(QString("%1").arg(row));
    56. widgetLineEedit->setText("QPushButton");
    57. }
    58.  
    59. void MainWindow::labelClicked(int row)
    60. {
    61. rowLineEdit->setText(QString("%1").arg(row));
    62. widgetLineEedit->setText("Custom QLabel");
    63. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. //CustomWidget.h
    2. class CustomWidget : public QLabel
    3. {
    4. Q_OBJECT
    5. public:
    6. explicit CustomWidget(const QString& text, QWidget *parent = 0);
    7.  
    8. signals:
    9. void released(void);
    10. void clicked(void);
    11.  
    12. protected:
    13. void mousePressEvent(QMouseEvent* e);
    14. void mouseReleaseEvent(QMouseEvent* e);
    15.  
    16. private:
    17. bool mousePressed;
    18. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. //CustomWidget.cpp
    2. CustomWidget::CustomWidget(const QString& text, QWidget* parent)
    3. : QLabel(text, parent)
    4. , mousePressed(false)
    5. {
    6. ;
    7. }
    8.  
    9. void CustomWidget::mousePressEvent(QMouseEvent* e)
    10. {
    11. mousePressed = true;
    12. }
    13.  
    14. void CustomWidget::mouseReleaseEvent(QMouseEvent* e)
    15. {
    16. emit released();
    17. if(mousePressed)
    18. {
    19. emit clicked();
    20. mousePressed = false;
    21. }
    22. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. //QLWIPushButton.h
    2. class QLWIPushButton : public QObject, public QListWidgetItem
    3. {
    4. Q_OBJECT;
    5. public:
    6. explicit QLWIPushButton(const QString text, QListWidget* view);
    7.  
    8. signals:
    9. void labelClicked(int row);
    10. void buttonClicked(int row);
    11.  
    12. private slots:
    13. void labelClick(void);
    14. void buttonClick(void);
    15. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. //QLWIPushButton.cpp
    2. QLWIPushButton::QLWIPushButton(const QString text, QListWidget* view)
    3. {
    4. QWidget* widget = new QWidget(view);
    5. QGridLayout* layout = new QGridLayout(widget);
    6.  
    7. QPushButton* button = new QPushButton(text, widget);
    8. CustomWidget* Label = new CustomWidget(text, widget);
    9.  
    10. connect(Label, SIGNAL(released()), this, SLOT(labelClick()));
    11. connect(button, SIGNAL(released()), this, SLOT(buttonClick()));
    12.  
    13. layout->addWidget(Label, 0, 0);
    14. layout->addWidget(button, 0, 1);
    15.  
    16. widget->setLayout(layout);
    17.  
    18. view->setItemWidget(this, widget);
    19. setSizeHint(widget->sizeHint());
    20. }
    21.  
    22. void QLWIPushButton::labelClick(void)
    23. {
    24. if(listWidget())
    25. emit labelClicked(listWidget()->row(this));
    26. }
    27.  
    28. void QLWIPushButton::buttonClick(void)
    29. {
    30. if(listWidget())
    31. emit buttonClicked(listWidget()->row(this));
    32. }
    To copy to clipboard, switch view to plain text mode 

  12. The following user says thank you to Santosh Reddy for this useful post:

    jindoniit (10th June 2011)

  13. #9
    Join Date
    Jun 2011
    Posts
    35
    Thanks
    16
    Qt products
    Qt3 Qt4
    Platforms
    Windows Symbian S60

    Default Re: How to set event click Custom ListView

    Thanks for your reply.
    but if i use widget to design ui , i can not custom row widget.
    i can not add label ,icon ,button on a row of widget.
    for example, i can see image below ,i want design it as below image
    Attached Images Attached Images
    Last edited by jindoniit; 10th June 2011 at 04:31.

  14. #10
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: How to set event click Custom ListView

    It should be possible to create this type of UI using QtDesigner, but you may need to use QTableWidget (instead of QListWidget). You can have three columns (icon column, text column, icon column), and set the icons in QtDesinger using icon resource files. In this case you can connect a your processing slot to
    Qt Code:
    1. clicked(const QModelIndex &index)
    To copy to clipboard, switch view to plain text mode 
    signal, which is emitted from QTableWidget.

    Still I can't think of a way to add QPushButton to item using QtDesigner

  15. The following user says thank you to Santosh Reddy for this useful post:

    jindoniit (10th June 2011)

  16. #11
    Join Date
    Jun 2011
    Posts
    35
    Thanks
    16
    Qt products
    Qt3 Qt4
    Platforms
    Windows Symbian S60

    Default Re: How to set event click Custom ListView

    this is my example. i use QAbstractItemDelegate to create item for Listview, and i also use QsKineticScroller to create scoll listview
    Attached Files Attached Files

  17. #12
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: How to set event click Custom ListView

    Ok, I have gone through example, and am able to run it on my Windows Vista machine (with some modifications to .pro file). It seems to work fine, downloads the icons, and adds to the items etc.

    Scrolling works, Clicking on items also work (you are already handling clicks on 30th row)

    So, what it that you want do now?

  18. The following user says thank you to Santosh Reddy for this useful post:

    jindoniit (10th June 2011)

  19. #13
    Join Date
    Jun 2011
    Posts
    35
    Thanks
    16
    Qt products
    Qt3 Qt4
    Platforms
    Windows Symbian S60

    Default Re: How to set event click Custom ListView

    Qt Code:
    1. QMouseEvent *me = (QMouseEvent*)event;
    2. if (iconPrevRect.x()<= me->pos().x() && me->pos().x()<=iconPrevRect.x()+iconPrevRect.width())
    3. {
    4. if (index.row()==30)
    5. {
    6. QMessageBox msgBox;
    7. msgBox.setInformativeText("Prev");
    8. msgBox.exec();
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 
    i handled event by: compare position when i click with position of image when i draw it on listview, it is only relatively
    I want to assign a event when i click on " prev image", but not based on its location

    if you have any way to solve this issue, please help me.
    Last edited by jindoniit; 10th June 2011 at 08:44.

  20. #14
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Cool Re: How to set event click Custom ListView

    Ok, here it is, have a look at the modifications, and take time to understand the modifications.

    example_worked.zip

    Good Luck.

  21. The following user says thank you to Santosh Reddy for this useful post:

    jindoniit (13th June 2011)

  22. #15
    Join Date
    Jun 2011
    Posts
    35
    Thanks
    16
    Qt products
    Qt3 Qt4
    Platforms
    Windows Symbian S60

    Default Re: How to set event click Custom ListView

    Thanks
    My issue is solved

Similar Threads

  1. inserting custom Widget to listview
    By Amit_3117 in forum Qt Programming
    Replies: 20
    Last Post: 27th August 2010, 17:38
  2. pushButton click event
    By aj2903 in forum Qt Programming
    Replies: 1
    Last Post: 9th June 2009, 11:19
  3. click Event on Title Bar
    By anupamgee in forum Qt Programming
    Replies: 1
    Last Post: 26th May 2009, 17:12
  4. On click event for a QPixmap
    By rishid in forum Qt Programming
    Replies: 1
    Last Post: 22nd February 2008, 16:12
  5. Custom Listview with all-time editable items
    By ivan.cukic in forum Qt Programming
    Replies: 5
    Last Post: 6th September 2006, 15:15

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.