目录
1. 原生QTableView显示数据
.h
#include <QWidget>
#include <QTableView>
#include <QStandardItemModel>
class MainWidget : public QWidget
{
Q_OBJECT
public:
MainWidget(QWidget *parent = nullptr);
~MainWidget();
void initTableView();
void initLayout();
private:
QTableView *m_pTableView;
QStandardItemModel *m_pViewModel;
};
.cpp
#include "MainWidget.h"
#include <QLayout>
#include <QHeaderView>
MainWidget::MainWidget(QWidget *parent)
: QWidget(parent)
{
initTableView();
initLayout();
setFixedWidth(400);
}
MainWidget::~MainWidget() {}
void MainWidget::initTableView()
{
m_pTableView = new QTableView;
QStringList headList;
headList << QStringLiteral("序号") << QStringLiteral("姓名") << QStringLiteral("性别");
m_pViewModel = new QStandardItemModel(this);
m_pViewModel->setColumnCount(3);
m_pViewModel->setHorizontalHeaderLabels(headList);
m_pTableView->setModel(m_pViewModel);
// 设置列宽相等 此时不可手动调整列宽
m_pTableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
// 设置表头对齐
m_pTableView->horizontalHeader()->setDefaultAlignment(Qt::AlignCenter);
// 设置隐藏表格线
//m_pTableView->setShowGrid(false);
// 设置选中整行
m_pTableView->setSelectionBehavior(QAbstractItemView::SelectRows);
// 设置一次只能选中一行
m_pTableView->setSelectionMode(QAbstractItemView::SingleSelection);
// 设置不可编辑
m_pTableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
// 设置选中后没有虚线
m_pTableView->setFocusPolicy(Qt::NoFocus);
// 设置取消垂直表头显示
m_pTableView->verticalHeader()->setVisible(false);
if (0)
{
// 指定行数添加数据
m_pViewModel->setRowCount(10);
for(int nRow = 0; nRow < 10; nRow++)
{
for(int nCol = 0; nCol < 3; nCol++)
{
QStandardItem *pItem = new QStandardItem;
pItem->setText(QString::number(nCol));
// 设置居中显示
pItem->setTextAlignment(Qt::AlignCenter);
m_pViewModel->setItem(nRow, nCol, pItem);
}
}
}
else
{
// 动态添加
for(int nRow = 0; nRow < 10; nRow++)
{
// int nRowCount = m_pViewModel->rowCount();
// m_pViewModel->insertRow(nRowCount);
QStandardItem *pItem1 = new QStandardItem;
pItem1->setText(QString::number(nRow+1));
pItem1->setTextAlignment(Qt::AlignCenter);
QStandardItem *pItem2 = new QStandardItem;
pItem2->setText(QStringLiteral("成功人士%1").arg(nRow));
pItem2->setTextAlignment(Qt::AlignCenter);
QStandardItem *pItem3 = new QStandardItem;
pItem3->setText(nRow % 2 == 0? QStringLiteral("女") : QStringLiteral("男"));
pItem3->setTextAlignment(Qt::AlignCenter);
QList<QStandardItem*> itemList;
itemList << pItem1 << pItem2 << pItem3;
m_pViewModel->appendRow(itemList);
}
}
}
void MainWidget::initLayout()
{
QVBoxLayout *pMainLayout = new QVBoxLayout;
pMainLayout->addWidget(m_pTableView);
setLayout(pMainLayout);
}
注意: 仅使用QStringLiteral()并不能保证中文不乱码,需要在CMAKE文件中指定 UTF-8
add_compile_options("$<$<CXX_COMPILER_ID:MSVC>:/source-charset:utf-8>")
CMakeLists 如下:
cmake_minimum_required(VERSION 3.5)
project(tableView VERSION 0.1 LANGUAGES CXX)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_compile_options("$<$<CXX_COMPILER_ID:MSVC>:/source-charset:utf-8>")
find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets)
set(PROJECT_SOURCES
main.cpp
MainWidget.cpp
MainWidget.h
)
if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
qt_add_executable(tableView
MANUAL_FINALIZATION
${PROJECT_SOURCES}
)
# Define target properties for Android with Qt 6 as:
# set_property(TARGET tableView APPEND PROPERTY QT_ANDROID_PACKAGE_SOURCE_DIR
# ${CMAKE_CURRENT_SOURCE_DIR}/android)
# For more information, see https://doc.qt.io/qt-6/qt-add-executable.html#target-creation
else()
if(ANDROID)
add_library(tableView SHARED
${PROJECT_SOURCES}
)
# Define properties for Android with Qt 5 after find_package() calls as:
# set(ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android")
else()
add_executable(tableView
${PROJECT_SOURCES}
)
endif()
endif()
target_link_libraries(tableView PRIVATE Qt${QT_VERSION_MAJOR}::Widgets)
# Qt for iOS sets MACOSX_BUNDLE_GUI_IDENTIFIER automatically since Qt 6.1.
# If you are developing for iOS or macOS you should consider setting an
# explicit, fixed bundle identifier manually though.
if(${QT_VERSION} VERSION_LESS 6.1.0)
set(BUNDLE_ID_OPTION MACOSX_BUNDLE_GUI_IDENTIFIER com.example.tableView)
endif()
set_target_properties(tableView PROPERTIES
${BUNDLE_ID_OPTION}
MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
MACOSX_BUNDLE TRUE
WIN32_EXECUTABLE TRUE
)
include(GNUInstallDirs)
install(TARGETS tableView
BUNDLE DESTINATION .
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
if(QT_VERSION_MAJOR EQUAL 6)
qt_finalize_executable(tableView)
endif()