Custom QListWidget: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
Line 10: | Line 10: | ||
# 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 | The header file | ||
<code> | |||
#ifndef MYLISTWIDGET_H | |||
#define MYLISTWIDGET_H | |||
#include | #include <QListWidget> | ||
class MyListWidget : public QListWidget | class MyListWidget : public QListWidget | ||
{ | |||
Q_OBJECT | |||
public: | public: | ||
Line 20: | Line 25: | ||
MyListWidget(QWidget *parent = 0); | MyListWidget(QWidget *parent = 0); | ||
protected: | protected: | ||
void dropEvent(QDropEvent *event); | |||
}; | |||
#endif // MYLISTWIDGET_H | #endif // MYLISTWIDGET_H | ||
</code> | |||
The cpp file | The cpp file | ||
<code> | |||
#include "mylistwidget.h" | |||
#include <QDebug> | |||
MyListWidget::MyListWidget(QWidget *parent) | MyListWidget::MyListWidget(QWidget *parent) | ||
: QListWidget(parent) | |||
{ | |||
setDragDropMode(QAbstractItemView::DragDrop); | |||
setDefaultDropAction(Qt::MoveAction); | |||
setAlternatingRowColors(true); | |||
} | |||
void MyListWidget::dropEvent(QDropEvent *event) | void MyListWidget::dropEvent(QDropEvent *event) | ||
{ | |||
qDebug() << "This is my custom dropEvent() method!"; | |||
QListWidget::dropEvent(event); | |||
} | |||
</code> |
Revision as of 10:38, 25 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
#ifndef MYLISTWIDGET_H
#define MYLISTWIDGET_H
#include <QListWidget>
class MyListWidget : public QListWidget
{
Q_OBJECT
public:
MyListWidget(QWidget *parent = 0);
protected:
void dropEvent(QDropEvent *event);
};
#endif // MYLISTWIDGET_H
The cpp file
#include "mylistwidget.h"
#include <QDebug>
MyListWidget::MyListWidget(QWidget *parent)
: QListWidget(parent)
{
setDragDropMode(QAbstractItemView::DragDrop);
setDefaultDropAction(Qt::MoveAction);
setAlternatingRowColors(true);
}
void MyListWidget::dropEvent(QDropEvent *event)
{
qDebug() << "This is my custom dropEvent() method!";
QListWidget::dropEvent(event);
}