Using various custom delegates in QTableView

Keywords: Qt

Train of thought:

1. Define delegation for each column:

The first column is a numbered column. Use a read-only delegate to make the cells in this column read-only
The third column is the ID column, which can only input 1-12 numbers. The qlinedit delegation and regular expression are used to restrict the input
In the fourth age column, QSpinBox delegation is used for input restriction, and only numbers between 1 and 100 can be entered
The fifth column is the gender column. The input is limited by QComboBox delegation. The cells in this column can only enter Male or Female
The sixth column is the head image column. Place a head image in the center of the cell of the column

2. Define a proxy class to display the characters in all cells in the middle.

3. Use QSS to make the background color of the table yellow and blue.

           

#include <QtGui>  
  
//Number column, read-only delegation  
//I can't think of this method, hehe  
class ReadOnlyDelegate : public QItemDelegate  
{  
    Q_OBJECT  
public:  
    ReadOnlyDelegate(QObject *parent = 0): QItemDelegate(parent) { }  
    QWidget *createEditor(QWidget*parent, const QStyleOptionViewItem &option,  
        const QModelIndex &index) const  
    {  
        return NULL;  
    }  
};  
  
//ID column, only 1-12 numbers can be entered  
//Using qlinedit delegate and regular expression to restrict input  
class UserIDDelegate : public QItemDelegate  
{  
    Q_OBJECT  
public:  
    UserIDDelegate(QObject *parent = 0): QItemDelegate(parent) { }  
    QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,  
        const QModelIndex &index) const  
    {  
        QLineEdit *editor = new QLineEdit(parent);  
        QRegExp regExp("[0-9]{0,10}");  
        editor->setValidator(new QRegExpValidator(regExp, parent));  
        return editor;  
    }  
    void setEditorData(QWidget *editor, const QModelIndex &index) const  
    {  
        QString text = index.model()->data(index, Qt::EditRole).toString();  
        QLineEdit *lineEdit = static_cast<QLineEdit*>(editor);  
        lineEdit->setText(text);  
    }  
    void setModelData(QWidget *editor, QAbstractItemModel *model,  
        const QModelIndex &index) const  
    {  
        QLineEdit *lineEdit = static_cast<QLineEdit*>(editor);  
        QString text = lineEdit->text();  
        model->setData(index, text, Qt::EditRole);  
    }  
    void updateEditorGeometry(QWidget *editor,  
        const QStyleOptionViewItem &option, const QModelIndex &index) const  
    {  
        editor->setGeometry(option.rect);  
    }  
};  
  
//For the age column, use the QSpinBox delegation to enter the limit. Only numbers between 1 and 100 can be entered  
class AgeDelegate : public QItemDelegate  
{  
    Q_OBJECT  
public:  
    AgeDelegate(QObject *parent = 0): QItemDelegate(parent) { }  
    QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,  
        const QModelIndex &index) const  
    {  
        QSpinBox *editor = new QSpinBox(parent);  
        editor->setMinimum(1);  
        editor->setMaximum(100);  
        return editor;  
    }  
    void setEditorData(QWidget *editor, const QModelIndex &index) const  
    {  
        int value = index.model()->data(index, Qt::EditRole).toInt();  
        QSpinBox *spinBox = static_cast<QSpinBox*>(editor);  
        spinBox->setValue(value);  
    }  
    void setModelData(QWidget *editor, QAbstractItemModel *model,  
        const QModelIndex &index) const  
    {  
        QSpinBox *spinBox = static_cast<QSpinBox*>(editor);  
        spinBox->interpretText();  
        int value = spinBox->value();  
        model->setData(index, value, Qt::EditRole);  
    }  
    void updateEditorGeometry(QWidget *editor,  
        const QStyleOptionViewItem &option, const QModelIndex &index) const  
    {  
        editor->setGeometry(option.rect);  
    }  
};  
  
//Gender column, using QComboBox delegation to restrict input  
//Cells in this column can only enter Male or Female  
class SexDelegate : public QItemDelegate  
{  
    Q_OBJECT  
public:  
    SexDelegate(QObject *parent = 0): QItemDelegate(parent) { }  
    QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,  
        const QModelIndex &index) const  
    {  
        QComboBox *editor = new QComboBox(parent);  
        editor->addItem("Female");  
        editor->addItem("Male");  
        return editor;  
    }  
    void setEditorData(QWidget *editor, const QModelIndex &index) const  
    {  
        QString text = index.model()->data(index, Qt::EditRole).toString();  
        QComboBox *comboBox = static_cast<QComboBox*>(editor);  
        int tindex = comboBox->findText(text);  
        comboBox->setCurrentIndex(tindex);  
    }  
    void setModelData(QWidget *editor, QAbstractItemModel *model,  
        const QModelIndex &index) const  
    {  
        QComboBox *comboBox = static_cast<QComboBox*>(editor);  
        QString text = comboBox->currentText();  
        model->setData(index, text, Qt::EditRole);  
    }  
    void updateEditorGeometry(QWidget *editor,  
        const QStyleOptionViewItem &option, const QModelIndex &index) const  
    {  
        editor->setGeometry(option.rect);  
    }  
};  
  
//The avatar column is just a small picture in the center of the cell  
class IconDelegate : public QItemDelegate  
{  
    Q_OBJECT  
public:  
    IconDelegate(QObject *parent = 0): QItemDelegate(parent) { }  
    void paint(QPainter *painter, const QStyleOptionViewItem &option,  
        const QModelIndex & index ) const  
    {  
        //show.bmp is a picture in the project directory (in fact, it is the QQ icon, ha ha)  
        QPixmap pixmap = QPixmap("show.bmp").scaled(24, 24);  
        qApp->style()->drawItemPixmap(painter, option.rect,  Qt::AlignCenter, QPixmap(pixmap));  
    }  
};  
  
//Proxy class, display the characters in all cells in the middle  
class VIPModel : public QStandardItemModel  
{  
    Q_OBJECT  
public:  
    VIPModel(QObject *parent=NULL) : QStandardItemModel(parent) { }  
    VIPModel(int row, int column, QObject *parent=NULL)  
        : QStandardItemModel(row, column, parent) { }  
    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const  
    {  
        if( Qt::TextAlignmentRole == role )  
            return Qt::AlignCenter;   
        return QStandardItemModel::data(index, role);  
    }  
  
};  
  
#include "main.moc"  
  
int main(int argc, char *argv[])  
{  
    QApplication app(argc, argv);  
  
    VIPModel *model = new VIPModel(5, 5);  
    QTableView *tableView = new QTableView;  
  
    //Set the background of the form to yellow and blue  
    //This method is seen on the Internet. It's very convenient to use  
    tableView->setAlternatingRowColors(true);  
    tableView->setStyleSheet("QTableView{background-color: rgb(250, 250, 115);"  
        "alternate-background-color: rgb(141, 163, 215);}");  
  
    tableView->setWindowTitle("VIP List");  
    tableView->resize(700, 400);  
    tableView->setModel(model);  
    QStringList headerList;  
    headerList << "No." << "ID" << "Name" << "Age" << "Sex" << "Show";  
    model->setHorizontalHeaderLabels(headerList);  
    tableView->verticalHeader()->setVisible(false);  
    tableView->horizontalHeader()->setStretchLastSection(true);  
  
    //Load delegates for each column  
    ReadOnlyDelegate readOnlyDelegate;  
    tableView->setItemDelegateForColumn(0, &readOnlyDelegate);  
    UserIDDelegate userIDDelegate;  
    tableView->setItemDelegateForColumn(1, &userIDDelegate);  
    AgeDelegate spinBoxDelegate;  
    tableView->setItemDelegateForColumn(3, &spinBoxDelegate);  
    SexDelegate comboBoxDelegate;  
    tableView->setItemDelegateForColumn(4, &comboBoxDelegate);  
    IconDelegate iconDelegate;  
    tableView->setItemDelegateForColumn(5, &iconDelegate);  
  
    for(int i=0; i<10; i++)  
    {  
        QModelIndex index = model->index(i, 0, QModelIndex());  
        model->setData(index, i);  
    }  
  
    tableView->show();  
    return app.exec();  
}  

 

Posted by timcclayton on Tue, 31 Dec 2019 02:45:01 -0800