一、列表框
#include "widget.h"
#include <QApplication>
#include <QListWidget>
#include <QStringList>
#include <QTableWidget>
#include <QTreeWidget>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
QListWidget lw(&w);
lw.move(100,200);
lw.resize(100,200);
lw.addItem("百度");
lw.addItem(new QListWidgetItem("腾讯"));
QStringList qstr;
qstr << "阿里巴巴" << "字节跳动" ;
lw.addItems(QStringList() << "阿里巴巴" << "字节跳动");
QObject::connect(&lw,&QListWidget::itemDoubleClicked,&lw,&QListWidget::clear);
w.show();
return a.exec();
}
输出:
二、表格
#include "widget.h"
#include <QApplication>
#include <QListWidget>
#include <QStringList>
#include <QTableWidget>
#include <QTreeWidget>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
QTableWidget tw(3,3,&w);
tw.move(100,100);
tw.resize(500,400);
tw.setHorizontalHeaderLabels(QStringList() << "企业" << "网址" << "创始人");
tw.setItem(0,0,new QTableWidgetItem("百度"));
tw.setItem(0,1,new QTableWidgetItem("www.baidu.com"));
tw.setItem(0,2,new QTableWidgetItem("李彦宏"));
tw.setItem(1,0,new QTableWidgetItem("腾讯"));
tw.setItem(1,1,new QTableWidgetItem("www.qq.com"));
tw.setItem(1,2,new QTableWidgetItem("马化腾"));
tw.setItem(2,0,new QTableWidgetItem("阿里巴巴"));
tw.setItem(2,1,new QTableWidgetItem("www.1688.com"));
tw.setItem(2,2,new QTableWidgetItem("马云"));
tw.setRowCount(tw.rowCount() + 1);
tw.setItem(3,0,new QTableWidgetItem("字节跳动"));
tw.setItem(3,1,new QTableWidgetItem("www.bytedance.com"));
tw.setItem(3,2,new QTableWidgetItem("张一鸣"));
QObject::connect(&tw,&QTableWidget::cellDoubleClicked,&tw,&QTableWidget::removeRow);
w.show();
return a.exec();
}
输出:
三、树形控件
#include "widget.h"
#include <QApplication>
#include <QListWidget>
#include <QStringList>
#include <QTableWidget>
#include <QTreeWidget>
tw.move(100,100);
tw.resize(300,200);
QTreeWidgetItem topItem(&tw);
topItem.setText(0,"网址");
//tw.addTopLevelItem(&topItem);
QTreeWidgetItem Bd(&topItem,QStringList() << "百度");
QTreeWidgetItem BdUrl(&Bd,QStringList() << "www.baidu.com");
QTreeWidgetItem Tx(&topItem,QStringList() << "腾讯");
QTreeWidgetItem TxUrl(&Tx,QStringList() << "www.qq.com");
w.show();
return a.exec();
}
输出: