QWidget* FormUiLoader::createWidget( const QString& className, QWidget* parent, const QString& name) { if (className == "QTreeWidget") { QTreeWidget* rt = new WidgetWithDragAndDrop<QTreeWidget>( m_dataLoader, parent, name, m_debug); rt->setDragEnabled( true); rt->showDropIndicator(); return rt; } else if (className == "QListWidget") { QListWidget* rt = new WidgetWithDragAndDrop<QListWidget>( m_dataLoader, parent, name, m_debug); rt->setDragEnabled( true); rt->showDropIndicator(); return rt; } else if (className == "QTableWidget") { QTableWidget* rt = new WidgetWithDragAndDrop<QTableWidget>( m_dataLoader, parent, name, m_debug); rt->setDragEnabled( true); return rt; } else { return QUiLoader::createWidget( className, parent, name); } }
MainWindow::MainWindow() { QMenu *fileMenu = new QMenu(tr("&File")); QAction *quitAction = fileMenu->addAction(tr("E&xit")); quitAction->setShortcut(tr("Ctrl+Q")); menuBar()->addMenu(fileMenu); // For convenient quoting: //! [0] QListWidget *listWidget = new QListWidget(this); listWidget->setSelectionMode(QAbstractItemView::SingleSelection); listWidget->setDragEnabled(true); listWidget->viewport()->setAcceptDrops(true); listWidget->setDropIndicatorShown(true); //! [0] //! [1] listWidget->setDragDropMode(QAbstractItemView::InternalMove); //! [1] this->listWidget = listWidget; connect(quitAction, SIGNAL(triggered()), this, SLOT(close())); setupListItems(); setCentralWidget(listWidget); setWindowTitle(tr("List Widget")); }
int main(int argc, char *argv[]) { QApplication app(argc, argv); // QListWidget QListWidget listWidget; // 一种添加项目的简便方法 new QListWidgetItem("a", &listWidget); // 添加项目的另一种方法,这样还可以进行各种设置 QListWidgetItem *listWidgetItem = new QListWidgetItem; listWidgetItem->setText("b"); listWidgetItem->setIcon(QIcon("../modelView2/yafeilinux.png")); listWidgetItem->setToolTip("this is b!"); listWidget.insertItem(1, listWidgetItem); // 设置排序为倒序 listWidget.sortItems(Qt::DescendingOrder); // 显示列表部件 listWidget.show(); // QTreeWidget QTreeWidget treeWidget; // 必须设置列数 treeWidget.setColumnCount(2); // 设置标头 QStringList headers; headers << "name" << "year"; treeWidget.setHeaderLabels(headers); // 添加项目 QTreeWidgetItem *grade1 = new QTreeWidgetItem(&treeWidget); grade1->setText(0,"Grade1"); QTreeWidgetItem *student = new QTreeWidgetItem(grade1); student->setText(0,"Tom"); student->setText(1,"1986"); QTreeWidgetItem *grade2 = new QTreeWidgetItem(&treeWidget, grade1); grade2->setText(0,"Grade2"); treeWidget.show(); // QTableWidget // 创建表格部件,同时指定行数和列数 QTableWidget tableWidget(3, 2); // 创建表格项目,并插入到指定单元 QTableWidgetItem *tableWidgetItem = new QTableWidgetItem("qt"); tableWidget.setItem(1, 1, tableWidgetItem); // 创建表格项目,并将它们作为标头 QTableWidgetItem *headerV = new QTableWidgetItem("first"); tableWidget.setVerticalHeaderItem(0,headerV); QTableWidgetItem *headerH = new QTableWidgetItem("ID"); tableWidget.setHorizontalHeaderItem(0,headerH); tableWidget.show(); // 为listWidget启用拖放 // 设置选择模式为单选 listWidget.setSelectionMode(QAbstractItemView::SingleSelection); // 启用拖动 listWidget.setDragEnabled(true); // 设置接受拖放 listWidget.viewport()->setAcceptDrops(true); // 设置显示将要被放置的位置 listWidget.setDropIndicatorShown(true); // 设置拖放模式为移动项目,如果不设置,默认为复制项目 listWidget.setDragDropMode(QAbstractItemView::InternalMove); return app.exec(); }