Custom QListWidget: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
=Custom QListWidget= | [[Category:snippets]] | ||
= Custom QListWidget = | |||
Small snippet showing how to override dropEvent method on a custom QListWidget. | Small snippet showing how to override dropEvent method on a custom QListWidget. | ||
# First create a Qt Gui Application using Qt Creator and add a QListWidget to it. | # First create a Qt Gui Application using Qt Creator and add a QListWidget to it. | ||
# Create the below given MyListWidget.h and MyListWidget.cpp files. | # Create the below given MyListWidget.h and MyListWidget.cpp files. | ||
# Right click on the QListWidget created in the Form designer and Promote it to MyListWidget | # Right click on the QListWidget created in the Form designer and Promote it to MyListWidget | ||
# When you run the application, if you drop something on MyListWidget, you will first see the debug msg (dropEvent of MyListWidget), and then it calls the dropEvent of the QListWidget … | # When you run the application, if you drop something on MyListWidget, you will first see the debug msg (dropEvent of MyListWidget), and then it calls the dropEvent of the QListWidget … | ||
The header file<br /> | The header file<br /><code><br />#ifndef MYLISTWIDGET_H<br />#define MYLISTWIDGET_H | ||
#include <QListWidget&gt; | |||
class MyListWidget : public QListWidget<br />{<br /> Q_OBJECT | |||
public: | |||
MyListWidget(QWidget *parent = 0); | |||
protected:<br /> void dropEvent(QDropEvent *event);<br />}; | |||
#endif // MYLISTWIDGET_H<br /></code> | |||
The cpp file<br /> | The cpp file<br /><code><br />#include "mylistwidget.h&quot;<br />#include <QDebug&gt; | ||
MyListWidget::MyListWidget(QWidget *parent)<br /> : QListWidget(parent)<br />{<br /> setDragDropMode(QAbstractItemView::DragDrop);<br /> setDefaultDropAction(Qt::MoveAction);<br /> setAlternatingRowColors(true);<br />} | |||
* | void MyListWidget::dropEvent(QDropEvent *event)<br />{<br /> qDebug() << "This is my custom dropEvent() method!";<br /> QListWidget::dropEvent(event);<br />}<br /></code> |
Revision as of 10:18, 24 February 2015
Custom QListWidget
Small snippet showing how to override dropEvent method on a custom QListWidget.
- First create a Qt Gui Application using Qt Creator and add a QListWidget to it.
- Create the below given MyListWidget.h and MyListWidget.cpp files.
- Right click on the QListWidget created in the Form designer and Promote it to MyListWidget
- When you run the application, if you drop something on MyListWidget, you will first see the debug msg (dropEvent of MyListWidget), and then it calls the dropEvent of the QListWidget …
The header file
<br />#ifndef MYLISTWIDGET_H<br />#define MYLISTWIDGET_H
#include <QListWidget&gt;
class MyListWidget : public QListWidget<br />{<br /> Q_OBJECT
public:
MyListWidget(QWidget *parent = 0);
protected:<br /> void dropEvent(QDropEvent *event);<br />};
#endif // MYLISTWIDGET_H<br />
The cpp file
<br />#include "mylistwidget.h&quot;<br />#include <QDebug&gt;
MyListWidget::MyListWidget(QWidget *parent)<br /> : QListWidget(parent)<br />{<br /> setDragDropMode(QAbstractItemView::DragDrop);<br /> setDefaultDropAction(Qt::MoveAction);<br /> setAlternatingRowColors(true);<br />}
void MyListWidget::dropEvent(QDropEvent *event)<br />{<br /> qDebug() << "This is my custom dropEvent() method!";<br /> QListWidget::dropEvent(event);<br />}<br />