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(); }
void setupUi(QDialog *MVPPlayerRemoteDialog) { if (MVPPlayerRemoteDialog->objectName().isEmpty()) MVPPlayerRemoteDialog->setObjectName(QStringLiteral("MVPPlayerRemoteDialog")); MVPPlayerRemoteDialog->resize(380, 396); MVPPlayerRemoteDialog->setAcceptDrops(true); verticalLayout_4 = new QVBoxLayout(MVPPlayerRemoteDialog); verticalLayout_4->setObjectName(QStringLiteral("verticalLayout_4")); verticalLayout_2 = new QVBoxLayout(); #ifndef Q_OS_MAC verticalLayout_2->setSpacing(-1); #endif verticalLayout_2->setObjectName(QStringLiteral("verticalLayout_2")); verticalLayout_2->setContentsMargins(-1, 0, 0, -1); verticalLayout_3 = new QVBoxLayout(); verticalLayout_3->setObjectName(QStringLiteral("verticalLayout_3")); verticalLayout_3->setSizeConstraint(QLayout::SetMinimumSize); horizontalLayout_4 = new QHBoxLayout(); horizontalLayout_4->setObjectName(QStringLiteral("horizontalLayout_4")); horizontalLayout_4->setSizeConstraint(QLayout::SetMinimumSize); sliderVolume = new QSlider(MVPPlayerRemoteDialog); sliderVolume->setObjectName(QStringLiteral("sliderVolume")); QSizePolicy sizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding); sizePolicy.setHorizontalStretch(0); sizePolicy.setVerticalStretch(0); sizePolicy.setHeightForWidth(sliderVolume->sizePolicy().hasHeightForWidth()); sliderVolume->setSizePolicy(sizePolicy); sliderVolume->setMinimumSize(QSize(0, 0)); sliderVolume->setMaximum(100); sliderVolume->setValue(100); sliderVolume->setOrientation(Qt::Horizontal); horizontalLayout_4->addWidget(sliderVolume); lblVol = new QLabel(MVPPlayerRemoteDialog); lblVol->setObjectName(QStringLiteral("lblVol")); QSizePolicy sizePolicy1(QSizePolicy::Maximum, QSizePolicy::Preferred); sizePolicy1.setHorizontalStretch(0); sizePolicy1.setVerticalStretch(0); sizePolicy1.setHeightForWidth(lblVol->sizePolicy().hasHeightForWidth()); lblVol->setSizePolicy(sizePolicy1); lblVol->setPixmap(QPixmap(QString::fromUtf8(":/mvpplayer/app/icon.png"))); lblVol->setAlignment(Qt::AlignCenter); horizontalLayout_4->addWidget(lblVol); verticalLayout_3->addLayout(horizontalLayout_4); layoutButtonsBar = new QHBoxLayout(); layoutButtonsBar->setObjectName(QStringLiteral("layoutButtonsBar")); btnServer = new QToolButton(MVPPlayerRemoteDialog); btnServer->setObjectName(QStringLiteral("btnServer")); QIcon icon; icon.addFile(QStringLiteral(":/mvpplayer/toolbar/connection.png"), QSize(), QIcon::Normal, QIcon::Off); btnServer->setIcon(icon); btnServer->setIconSize(QSize(32, 32)); btnServer->setCheckable(true); layoutButtonsBar->addWidget(btnServer); verticalLayout_3->addLayout(layoutButtonsBar); verticalLayout_2->addLayout(verticalLayout_3); line_2 = new QFrame(MVPPlayerRemoteDialog); line_2->setObjectName(QStringLiteral("line_2")); line_2->setFrameShape(QFrame::HLine); line_2->setFrameShadow(QFrame::Sunken); verticalLayout_2->addWidget(line_2); cbMute = new QCheckBox(MVPPlayerRemoteDialog); cbMute->setObjectName(QStringLiteral("cbMute")); cbMute->setChecked(true); verticalLayout_2->addWidget(cbMute); verticalLayout = new QVBoxLayout(); verticalLayout->setObjectName(QStringLiteral("verticalLayout")); verticalLayout->setSizeConstraint(QLayout::SetNoConstraint); horizontalLayout_2 = new QHBoxLayout(); horizontalLayout_2->setObjectName(QStringLiteral("horizontalLayout_2")); label = new QLabel(MVPPlayerRemoteDialog); label->setObjectName(QStringLiteral("label")); horizontalLayout_2->addWidget(label); lblCurrentTrack = new QLabel(MVPPlayerRemoteDialog); lblCurrentTrack->setObjectName(QStringLiteral("lblCurrentTrack")); QSizePolicy sizePolicy2(QSizePolicy::MinimumExpanding, QSizePolicy::Minimum); sizePolicy2.setHorizontalStretch(0); sizePolicy2.setVerticalStretch(0); sizePolicy2.setHeightForWidth(lblCurrentTrack->sizePolicy().hasHeightForWidth()); lblCurrentTrack->setSizePolicy(sizePolicy2); lblCurrentTrack->setWordWrap(true); horizontalLayout_2->addWidget(lblCurrentTrack); lblTrackLength = new QLabel(MVPPlayerRemoteDialog); lblTrackLength->setObjectName(QStringLiteral("lblTrackLength")); lblTrackLength->setAlignment(Qt::AlignCenter); horizontalLayout_2->addWidget(lblTrackLength); verticalLayout->addLayout(horizontalLayout_2); horizontalLayout_3 = new QHBoxLayout(); horizontalLayout_3->setObjectName(QStringLiteral("horizontalLayout_3")); sliderPosition = new QSlider(MVPPlayerRemoteDialog); sliderPosition->setObjectName(QStringLiteral("sliderPosition")); sliderPosition->setMaximum(100); sliderPosition->setOrientation(Qt::Horizontal); sliderPosition->setInvertedAppearance(false); sliderPosition->setInvertedControls(false); sliderPosition->setTickPosition(QSlider::TicksBelow); sliderPosition->setTickInterval(1); horizontalLayout_3->addWidget(sliderPosition); verticalLayout->addLayout(horizontalLayout_3); horizontalLayout_5 = new QHBoxLayout(); horizontalLayout_5->setObjectName(QStringLiteral("horizontalLayout_5")); btnClearPlaylist = new QToolButton(MVPPlayerRemoteDialog); btnClearPlaylist->setObjectName(QStringLiteral("btnClearPlaylist")); QIcon icon1; icon1.addFile(QStringLiteral(":/mvpplayer/action/cancel.png"), QSize(), QIcon::Normal, QIcon::Off); btnClearPlaylist->setIcon(icon1); horizontalLayout_5->addWidget(btnClearPlaylist); lblPlaylist = new QLabel(MVPPlayerRemoteDialog); lblPlaylist->setObjectName(QStringLiteral("lblPlaylist")); horizontalLayout_5->addWidget(lblPlaylist); verticalLayout->addLayout(horizontalLayout_5); playlist = new QListWidget(MVPPlayerRemoteDialog); playlist->setObjectName(QStringLiteral("playlist")); QSizePolicy sizePolicy3(QSizePolicy::Expanding, QSizePolicy::Maximum); sizePolicy3.setHorizontalStretch(0); sizePolicy3.setVerticalStretch(0); sizePolicy3.setHeightForWidth(playlist->sizePolicy().hasHeightForWidth()); playlist->setSizePolicy(sizePolicy3); playlist->setDragDropMode(QAbstractItemView::DropOnly); verticalLayout->addWidget(playlist); verticalLayout_2->addLayout(verticalLayout); verticalLayout_4->addLayout(verticalLayout_2); QWidget::setTabOrder(cbMute, sliderPosition); QWidget::setTabOrder(sliderPosition, playlist); retranslateUi(MVPPlayerRemoteDialog); QMetaObject::connectSlotsByName(MVPPlayerRemoteDialog); } // setupUi