In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains the "C++ and Qt TableDelegate how to customize agent components", the article explains the content is simple and clear, easy to learn and understand, now please follow the editor's ideas slowly in depth, together to study and learn "C++ and Qt TableDelegate how to customize agent components" bar!
The main role of TableDelegate custom agent component is to adjust the original table, for example, by default, the default agent in Table is an edit box, we can only enter data in the edit box, and sometimes we want to select data instead of input, we need to rewrite the edit box to achieve the effect of selection. Agent components are often used to customize field types in Table tables.
QAbstractItemDelegate is the abstract base class of all proxy classes in the custom proxy. When we inherit any component, we must include the following four functions:
CreateEditor () is used to create components that edit model data, such as (QSpinBox component)
SetEditorData () fetches data from the data model for editing by the Widget component
SetModelData () updates the data on the Widget component to the data model
UpdateEditorGeometry () sets an appropriate size for the Widget component
Here we rewrite three proxy interfaces, of which two ComBox components are used to select marriage or not, and the SpinBox component is used to adjust the numerical range. First, three rewriting components are defined.
The rewrite interface spindelegate.cpp code is as follows.
# include "spindelegate.h" # include QWIntSpinDelegate::QWIntSpinDelegate (QObject * parent): QStyledItemDelegate (parent) {} / / https://www.cnblogs.com/lysharkQWidget * QWIntSpinDelegate::createEditor (QWidget * parent,const QStyleOptionViewItem & option, const QModelIndex & index) const {/ / create a proxy editing component Q_UNUSED (option); Q_UNUSED (index); QSpinBox * editor = new QSpinBox (parent); / / create a QSpinBox editor- > setFrame (false) / / set to borderless editor- > setMinimum (0); editor- > setMaximum (10000); return editor; / / return this editor} void QWIntSpinDelegate::setEditorData (QWidget * editor,const QModelIndex & index) const {/ / get the data from the data model and display the data int value = index.model ()-> data (index, Qt::EditRole). ToInt () QSpinBox * spinBox = static_cast (editor); / / cast spinBox- > setValue (value); / / set the editor's value} void QWIntSpinDelegate::setModelData (QWidget * editor, QAbstractItemModel * model, const QModelIndex & index) const {/ / save the data of the proxy component to the data model QSpinBox * spinBox = static_cast (editor); / / cast spinBox- > interpretText () / / interpret the data, if the data is modified, trigger the signal int value = spinBox- > value (); / / get the value of spinBox model- > setData (index, value, Qt::EditRole); / / Update to the data model} void QWIntSpinDelegate::updateEditorGeometry (QWidget * editor, const QStyleOptionViewItem & option, const QModelIndex & index) const {/ / set component size Q_UNUSED (index); editor- > setGeometry (option.rect);}
The rewrite interface floatspindelegate.cpp code is as follows.
# include "floatspindelegate.h" # include QWFloatSpinDelegate::QWFloatSpinDelegate (QObject * parent): QStyledItemDelegate (parent) {} QWidget * QWFloatSpinDelegate::createEditor (QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index) const {Q_UNUSED (option); Q_UNUSED (index); QDoubleSpinBox * editor = new QDoubleSpinBox (parent); editor- > setFrame (false); editor- > setMinimum (0); editor- > setDecimals (2); editor- > setMaximum (10000); return editor } void QWFloatSpinDelegate::setEditorData (QWidget * editor, const QModelIndex & index) const {float value = index.model ()-> data (index, Qt::EditRole). ToFloat (); QDoubleSpinBox * spinBox = static_cast (editor); spinBox- > setValue (value);} / / https://www.cnblogs.com/lysharkvoid QWFloatSpinDelegate::setModelData (QWidget * editor, QAbstractItemModel * model, const QModelIndex & index) const {QDoubleSpinBox * spinBox = static_cast (editor); spinBox- > interpretText () Float value = spinBox- > value (); QString str=QString::asprintf (".2f", value); model- > setData (index, str, Qt::EditRole);} void QWFloatSpinDelegate::updateEditorGeometry (QWidget * editor, const QStyleOptionViewItem & option, const QModelIndex & index) const {editor- > setGeometry (option.rect);}
The rewrite interface comboxdelegate.cpp code is as follows.
# include "comboxdelegate.h" # include QWComboBoxDelegate::QWComboBoxDelegate (QObject * parent): QItemDelegate (parent) {} QWidget * QWComboBoxDelegate::createEditor (QWidget * parent,const QStyleOptionViewItem & option, const QModelIndex & index) const {QComboBox * editor = new QComboBox (parent); editor- > addItem ("married"); editor- > addItem ("unmarried"); editor- > addItem ("single"); return editor } / / https://www.cnblogs.com/lysharkvoid QWComboBoxDelegate::setEditorData (QWidget * editor, const QModelIndex & index) const {QString str = index.model ()-> data (index, Qt::EditRole). ToString (); QComboBox * comboBox = static_cast (editor); comboBox- > setCurrentText (str);} void QWComboBoxDelegate::setModelData (QWidget * editor, QAbstractItemModel * model, const QModelIndex & index) const {QComboBox * comboBox = static_cast (editor); QString str = comboBox- > currentText () Model- > setData (index, str, Qt::EditRole);} void QWComboBoxDelegate::updateEditorGeometry (QWidget * editor,const QStyleOptionViewItem & option, const QModelIndex & index) const {editor- > setGeometry (option.rect);}
Import the widget into mainwindow.cpp and use ui- > tableView- > setItemDelegateForColumn; associate the widget to the specified table subscript index.
# include "mainwindow.h" # include "ui_mainwindow.h" / / https://www.cnblogs.com/lysharkMainWindow::MainWindow(QWidget * parent): QMainWindow (parent), ui (new Ui::MainWindow) {ui- > setupUi (this); / / initialize model data model = new QStandardItemModel; / / initialize 4 rows with six columns selection = new QItemSelectionModel (model); / / Association model ui- > tableView- > setModel (model) Ui- > tableView- > setSelectionModel (selection); / / add header QStringList HeaderList; HeaderList
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.