Custom QListWidget: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
(Added LangSwitch)
 
(3 intermediate revisions by 3 users not shown)
Line 1: Line 1:
{{LangSwitch}}
[[Category:snippets]]
[[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.


Line 10: Line 8:
# 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 /><code><br />#ifndef MYLISTWIDGET_H<br />#define MYLISTWIDGET_H
The header file
<code>
#ifndef MYLISTWIDGET_H
#define MYLISTWIDGET_H


#include &lt;QListWidget&amp;gt;
#include <QListWidget>


class MyListWidget : public QListWidget<br />{<br /> Q_OBJECT
class MyListWidget : public QListWidget
{
Q_OBJECT


public:
public:
Line 20: Line 23:
MyListWidget(QWidget *parent = 0);
MyListWidget(QWidget *parent = 0);


protected:<br /> void dropEvent(QDropEvent *event);<br />};
protected:
void dropEvent(QDropEvent *event);
};


#endif // MYLISTWIDGET_H<br /></code>
#endif // MYLISTWIDGET_H
</code>


The cpp file<br /><code><br />#include &quot;mylistwidget.h&amp;quot;<br />#include &lt;QDebug&amp;gt;
The cpp file
<code>
#include "mylistwidget.h"
#include <QDebug>


MyListWidget::MyListWidget(QWidget *parent)<br /> : QListWidget(parent)<br />{<br /> setDragDropMode(QAbstractItemView::DragDrop);<br /> setDefaultDropAction(Qt::MoveAction);<br /> setAlternatingRowColors(true);<br />}
MyListWidget::MyListWidget(QWidget *parent)
: QListWidget(parent)
{
setDragDropMode(QAbstractItemView::DragDrop);
setDefaultDropAction(Qt::MoveAction);
setAlternatingRowColors(true);
}


void MyListWidget::dropEvent(QDropEvent *event)<br />{<br /> qDebug() &lt;&lt; &quot;This is my custom dropEvent() method!&quot;;<br /> QListWidget::dropEvent(event);<br />}<br /></code>
void MyListWidget::dropEvent(QDropEvent *event)
{
qDebug() << "This is my custom dropEvent() method!";
QListWidget::dropEvent(event);
}
</code>

Latest revision as of 13:19, 28 June 2015

En Ar Bg De El Es Fa Fi Fr Hi Hu It Ja Kn Ko Ms Nl Pl Pt Ru Sq Th Tr Uk Zh

Small snippet showing how to override dropEvent method on a custom QListWidget.

  1. First create a Qt Gui Application using Qt Creator and add a QListWidget to it.
  2. Create the below given MyListWidget.h and MyListWidget.cpp files.
  3. Right click on the QListWidget created in the Form designer and Promote it to MyListWidget
  4. 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);
}