- All (478)
- jom (0)
- Qt Linguist (7)
- Qt Eclipse Integration (9)
- Qt Designer (7)
- Qt Creator (4)
- Qt build system: qmake (31)
- Qt build system: configure (3)
- Qt Assistant (5)
- Printing (4)
- Porting from Qt 3 to Qt 4 (1)
- Plugins (7)
- Qt Visual Studio AddIn (2)
- Qt/MFC Migration (2)
- QtScript (3)
- MDI (2)
- XML (1)
- Widgets (22)
- WebKit (5)
- Tools and Containers (2)
- Threads (2)
- Text Handling (10)
- SQL (6)
- QtTest (1)
- QtService (1)
- Platform: Windows (49)
- Platform: Unix (16)
- Platform: Mac OS X (18)
- Image Formats (2)
- I/O (2)
- Graphicsview (8)
- Font handling (9)
- Event System (18)
- Drag and Drop (4)
- Dialogs (6)
- Desktop integration (3)
- ActiveQt (3)
- Itemviews (60)
- Layout (4)
- Qt Quick (10)
- Qt SDK (1)
- Licensing (2)
- Platform: Embedded Linux (38)
- Painting (32)
- OpenGL (4)
- Object Model (6)
- Network (5)
- Multimedia (3)
- Miscellanous (23)
- Main Window (19)
- Look and Feel (23)
- Development (0)
- Getting Involved (0)
- Routines (0)
How can I drag from e.g a QListWidget and drop in an editable QTableView?
In order to enable dragging from the QListWidget, you need to reimplement dragEnterEvent() [qt.nokia.com] and dragMoveEvent() [qt.nokia.com] to accept the event. In addition you need to set setDragEnabled() [qt.nokia.com] to true to enable dragging of the view’s items.
For the QTableView you need to set setAcceptDrops() [qt.nokia.com] to true in order to allow drops and then set a model on the view that has reimplemented setData() [qt.nokia.com] and flags() [qt.nokia.com] to allow editing of the model.
The example below illustrates how this can be implemented:
- #include <QtCore>
- {
- public:
- protected:
- {
- e->accept();
- }
- {
- e->accept();
- }
- };
- {
- addItem("Zero");
- addItem("First");
- addItem("Second");
- addItem("Third");
- addItem("Fourth");
- // The tree supports dragging of its own items
- setDragEnabled(true);
- }
- {
- public:
- {
- QStringList firstRow;
- QStringList secondRow;
- for (int i = 0; i < 5; i++ ) {
- }
- stringList << firstRow << secondRow;
- }
- // Returns the number of rows
- {
- return 2;
- }
- // Returns the number of columns
- {
- return 5;
- }
- // Returns an appropriate value for the requested data.
- // If the view requests an invalid index or if the role is not
- // Qt::DisplayRole, an invalid variant is returned.
- // Any valid index that corresponds to a string for the index's column and row in
- // the stringlist is returned
- {
- if (!index.isValid())
- return list.at(index.column());
- }
- // Changes an item in the string list, but only if the following conditions
- // are met:
- // * The index supplied is valid.
- // * The index corresponds to an item to be shown in a view.
- // * The role associated with rendering data is specified.
- // The dataChanged() signal is emitted if the item is changed.
- {
- // Set the data in the current cell to be value
- stringList[index.row()].replace(index.column(), value.toString());
- // We only want to change the data for the current cell
- emit dataChanged(index, index);
- return true;
- }
- return true;
- }
- // Return the default flags for this index in addition to flags
- // that will accept drops and editing
- {
- }
- private:
- // Each row will consist of a list of strings
- };
- int main(int argc, char **argv)
- {
- QWidget widget;
- ListWidget *listWidget = new ListWidget(&widget);
- DragModel *model = new DragModel(tableView);
- tableView->setModel(model);
- // The table view can accept drops
- tableView->setAcceptDrops(true);
- layout->addWidget(listWidget);
- layout->addWidget(tableView);
- widget.show();
- return app.exec();
- }

No comments