QT中的QTableView的使用

void MainWindow::initCtrl()
{
	//下面是初始化 tableView_include 
	if(tableview_model==NULL) {tableview_model = new QStandardItemModel(this);}
	QList  items;
	QStandardItem *pcolumn1=new QStandardItem();
	items.append(pcolumn1);
	tableview_model->appendColumn(items);
	tableview_model->setHeaderData(0,Qt::Horizontal,"ICE files");
	ui->tableView_include->setModel( tableview_model );
	delete pcolumn1;
}

void MainWindow::on_pushButton_clicked()
{
	QStringList mimeTypeFilters;
	mimeTypeFilters << "ice/ice"; 

	QFileDialog dialog(this);
	dialog.setNameFilter("*.ice");
	dialog.exec();
	QStringList fileNames;
	fileNames = dialog.selectedFiles();
	QDir dir=dialog.directory();
	QFileInfo qfinfo(fileNames[0]);
	QString filename=qfinfo.filePath();//qfinfo.fileName(); 
	//如果filename已经在qsicefile中,那么不进行处理,不 重复添加
	if (qsicefile.contains(filename)==true)
	{
		return;
	}
	qsicefile.append(filename);
	//下面给tableView_include添加数据
	QStandardItem *_picefile=new QStandardItem(filename);
	tableview_model->appendRow(_picefile);
	picefile.append(_picefile);
}

QT中的QTableView的使用_第1张图片


参考:
http://blog.sina.com.cn/s/blog_a6fb6cc90101iang.html
C:\Qt\Qt5.5.1\Examples\Qt-5.5\widgets\itemviews\coloreditorfactory\coloreditorfactory.pro
C:\Qt\Qt5.5.1\Examples\Qt-5.5\widgets\itemviews\spinboxdelegate\spinboxdelegate.pro
C:\Qt\Qt5.5.1\Examples\Qt-5.5\widgets\itemviews\stardelegate\stardelegate.pro


QT中用TABVIEW显示一个QCOMBOX的例子1: 工程 spinboxdelegate
spinboxdelegate.pro:
QT += widgets
HEADERS     = delegate.h \
    tablemodel.h
SOURCES     = delegate.cpp \
              main.cpp \
    tablemodel.cpp
# install
target.path = $$[QT_INSTALL_EXAMPLES]/widgets/itemviews/spinboxdelegate
INSTALLS += target


delegate.h:
#ifndef DELEGATE_H
#define DELEGATE_H
#include
#include
class SpinBoxDelegate : public QStyledItemDelegate
{
    Q_OBJECT
public:
    SpinBoxDelegate(QObject *parent = 0);
    QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,const QModelIndex &index) const Q_DECL_OVERRIDE;
    void setEditorData(QWidget *editor, const QModelIndex &index) const Q_DECL_OVERRIDE;
    void setModelData(QWidget *editor, QAbstractItemModel *model,const QModelIndex &index) const Q_DECL_OVERRIDE;
    void updateEditorGeometry(QWidget *editor,const QStyleOptionViewItem &option, const QModelIndex &index) const Q_DECL_OVERRIDE;
private slots:
    void commitAndCloseEditor(const QString &s);
};
#endif


tablemodel.h:
#ifndef SMSDTABLEMODEL_H
#define SMSDTABLEMODEL_H
#include
class SMSDTableModel : public QAbstractTableModel
{
    Q_OBJECT
public:
    explicit SMSDTableModel(QObject *parent = 0);
    ~SMSDTableModel(void);
    int rowCount(const QModelIndex &parent) const Q_DECL_OVERRIDE;
    int columnCount(const QModelIndex &parent) const Q_DECL_OVERRIDE;
    bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole)Q_DECL_OVERRIDE;
    QVariant data(const QModelIndex &index, int role) const Q_DECL_OVERRIDE;
    Qt::ItemFlags flags(const QModelIndex &index) const Q_DECL_OVERRIDE;
    QVariant headerData(int section, Qt::Orientation orientation, int role) const Q_DECL_OVERRIDE;
public:
    void setShowData(QVector &data) ;
    void setComboxData(QVector &data) ;
    void setHorizontalHeader(const QStringList& headers);
private:
    QStringList m_HorizontalHeader;
    QVector m_TableModelShowData;
    QVector m_ComboxShowData;
    int comboCol;
};
#endif


delegate.cpp:
#include "delegate.h"
#include
#include


SpinBoxDelegate::SpinBoxDelegate(QObject *parent)
    : QStyledItemDelegate(parent)
{
}


QWidget *SpinBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem & option , const QModelIndex & index ) const
{
    QComboBox  *editor = new QComboBox(parent);
    editor->setFrame(false);
    connect(editor, SIGNAL(currentTextChanged(const QString &)),
            this, SLOT(commitAndCloseEditor(const QString &)));
//    QSpinBox *editor = new QSpinBox(parent);
//    editor->setFrame(false);
//    editor->setMinimum(0);
//    editor->setMaximum(100);
    return editor;
}


void SpinBoxDelegate::setEditorData(QWidget *editor,
                                    const QModelIndex &index) const
{
    QStringList value = index.model()->data(index, Qt::EditRole).toStringList();
    QString curvalue = index.model()->data(index, Qt::DisplayRole).toString();


    QComboBox *spinBox = static_cast(editor);
    spinBox->addItems(value);
    spinBox->setCurrentText(curvalue);
    //spinBox->setValue(value);
//    int value = index.model()->data(index, Qt::EditRole).toInt();
//    QSpinBox *spinBox = static_cast(editor);
//    spinBox->setValue(value);
}


void SpinBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
                                   const QModelIndex &index) const
{
    QComboBox *spinBox = static_cast(editor);


    QStringList value = model->data(index, Qt::EditRole).toStringList();
    model->setData(index, value, Qt::EditRole);
    //spinBox->interpretText();
    //QStringList sl = spinBox->item
    QString scur = spinBox->currentText();
    qDebug()<<"SpinBoxDelegate::setModelData"<     model->setData(index, scur, Qt::DisplayRole);
    //int value = spinBox->value();
    //model->setData(index, scur, Qt::DisplayRole);
//    QSpinBox *spinBox = static_cast(editor);
//    spinBox->interpretText();
//    int value = spinBox->value();
//    model->setData(index, value, Qt::EditRole);
}


void SpinBoxDelegate::updateEditorGeometry(QWidget *editor,const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
{
    editor->setGeometry(option.rect);
}


void SpinBoxDelegate::commitAndCloseEditor(const QString & s)
{
    QComboBox *editor = qobject_cast(sender());
    QString scur = editor->currentText();
qDebug()<<"SpinBoxDelegate::commitAndCloseEditor"<     emit commitData(editor);
    //emit closeEditor(editor);
}


tablemodel.cpp:
#include "tablemodel.h"
#include "QDebug"


SMSDTableModel::SMSDTableModel(QObject *parent) :
    QAbstractTableModel(parent)
{
    comboCol=1;
}


SMSDTableModel::~SMSDTableModel()
{


}




int SMSDTableModel::rowCount(const QModelIndex &parent) const
{
    Q_UNUSED( parent)
    int i=m_TableModelShowData.size();
    return i;
}


int SMSDTableModel::columnCount(const QModelIndex &parent) const
{
    Q_UNUSED( parent)
    int j=m_TableModelShowData.size();
    return j;
}


bool SMSDTableModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    /*if (!index.isValid())
        return false;


    int r=index.row();
    int c=index.column();
    QStringList tempList1=m_TableModelShowData.at(r);
    if(role == Qt::EditRole&&c!=comboCol)
    {
        QAbstractTableModel.setData(index,tempList1.at(0),Qt::EditRole);
        return true;
    }
    if(role == Qt::EditRole&&c==comboCol)
    {
        QStringList inner;
        inner<         QAbstractTableModel.setData(index,inner,Qt::EditRole);
        return true;
    }*/
     return false;
}


QVariant SMSDTableModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid())
        return QVariant();


    int r=index.row();
    int c=index.column();
    QStringList tempList1=m_TableModelShowData.at(r);
    QString sCombox = m_ComboxShowData.at(r);


    if(role == Qt::EditRole&&c!=comboCol)
    {
        QString stemp  = tempList1.at(0);
        return QVariant(stemp);
    }
    if(role == Qt::EditRole&&c==comboCol)
    {
        QStringList inner;
        inner<         return QVariant(inner);
    }
    if(role == Qt::DisplayRole&&c==comboCol)
    {
        //QString innerx;
        return QVariant(sCombox);
//        QVariant va=this->data(index,Qt::DisplayRole);
//        QString innerx = va.toString();
//        qDebug()<<"SMSDTableModel::data"< //        //inner< //        return QVariant(innerx);
    }
    //model->setData(index, scur, Qt::DisplayRole);
    return QVariant();
}


Qt::ItemFlags SMSDTableModel::flags(const QModelIndex &index) const
{
    if (!index.isValid())
        return Qt::NoItemFlags;


    Qt::ItemFlags flag = QAbstractItemModel::flags(index);


    flag|=Qt::ItemIsEditable; // 设置单元格可编辑,此处注释,单元格无法被编辑
    return flag;
}


void SMSDTableModel::setHorizontalHeader(const QStringList &headers)
{
    m_HorizontalHeader =  headers;
}




QVariant SMSDTableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    int m=m_HorizontalHeader.size();
    if(m==0) return QVariant();
    if (role == Qt::DisplayRole && orientation == Qt::Horizontal) {
        QString ss=m_HorizontalHeader.at(section);
        //QString strTemp=QString::fromLocal8Bit(ss.c_str());
        QString strTemp=ss;
        //QString strTemp(ss.c_str());
        QVariant vaTemp(strTemp);
        return vaTemp;
    }
    return QAbstractTableModel::headerData(section, orientation, role);
}


void SMSDTableModel::setShowData(QVector &data)
{
    m_TableModelShowData.clear();
    m_TableModelShowData=data;
}


void SMSDTableModel::setComboxData(QVector &data)
{
    m_ComboxShowData.clear();
    m_ComboxShowData=data;
}


main.cpp:
#include "delegate.h"
#include
#include
#include
#include
#include


int main(int argc, char *argv[])
{
    QApplication app(argc, argv);


    QTableView tableView;
    SMSDTableModel model(&tableView);


    QStringList tempList0;
    tempList0<<"000xxx"<<"000yyy"<<"000zzz"<<"000ttt";


    QStringList tempList1;
    tempList1<<"111xxx"<<"111yyy"<<"111zzz"<<"111ttt";


    QVector vecAlldata;
    vecAlldata.append(tempList0);
    vecAlldata.append(tempList1);
    model.setShowData(vecAlldata);


    QVector Comboxdata;
    Comboxdata.append(tempList0.at(2));
    Comboxdata.append(tempList1.at(2));
    model.setComboxData(Comboxdata);


    QStringList heas;
    heas<< QString("xxx")<< QString("yyyy");
    model.setHorizontalHeader(heas);
    tableView.horizontalHeader()->setStretchLastSection(true);


    SpinBoxDelegate delegate;
    //tableView.setItemDelegate(&delegate);
    tableView.setItemDelegateForColumn(1,&delegate);


    tableView.setModel(&model);


//    QModelIndex index01 = model.index(0, 1, QModelIndex());
//    model.setData(index01,tempList0,Qt::EditRole);
//    model.setData(index01,tempList0.at(0),Qt::DisplayRole);


//    QModelIndex index02 = model.index(1, 1, QModelIndex());
//    model.setData(index02,tempList1,Qt::EditRole);
//    model.setData(index02,tempList1.at(0),Qt::DisplayRole);
    /*for (int row = 0; row < 2; ++row) {
        for (int column = 1; column < 2; ++column) {
            QModelIndex index = model.index(row, column, QModelIndex());
            QString strTemp;
            strTemp.arg("%d-%d").arg(row).arg(column);
            //model.setData(index,tempList0)
            //model.setData(index, QVariant((row + 1) * (column + 1)));
        }
//! [1] //! [2]
    }*/
    tableView.setWindowTitle(QObject::tr("Spin Box Delegate"));
    tableView.show();
    return app.exec();
}


QT中用TABVIEW显示一个QCOMBOX的例子2: 工程 工程:coloreditorfactory
coloreditorfactory.pro
QT += widgets
HEADERS    = colorlisteditor.h \
     window.h
SOURCES    = colorlisteditor.cpp \
     window.cpp \
     main.cpp
# install
target.path = $$[QT_INSTALL_EXAMPLES]/widgets/itemviews/coloreditorfactory
INSTALLS += target


colorlisteditor.h:
#ifndef COLORLISTEDITOR_H
#define COLORLISTEDITOR_H


#include
#include


QT_BEGIN_NAMESPACE
class QColor;
class QWidget;
QT_END_NAMESPACE


//! [0]
class ColorListEditor : public QComboBox
{
    Q_OBJECT
    //Q_PROPERTY(QColor ljtcolor READ getcolor WRITE setColor USER true)
    Q_PROPERTY(QString xxComboStr READ getComboStr WRITE setComboStr USER true)
public:
    ColorListEditor(QWidget *widget = 0);
public:
//    QColor getcolor() const;
//    void setColor(QColor c);


    QString getComboStr() const;
    void setComboStr(QString c);
private:
    void populateList();
    QVector myAllComboData;
};
//! [0]
#endif


window.h:
#ifndef WINDOW_H
#define WINDOW_H
#include >
#include


class Window : public QWidget
{
    Q_OBJECT


public:
    Window();
public slots:
    void openPersistentEditor(QTableWidgetItem *item);
    void slotSelectionChanged();
    void oxkslot();
private:
    void createGUI();
    QTableWidget *table;
    QString getCurcobo;
};
#endif


colorlisteditor.cpp:
#include


#include "colorlisteditor.h"


ColorListEditor::ColorListEditor(QWidget *widget) : QComboBox(widget)
{
    populateList();
}


QString ColorListEditor::getComboStr() const
{
    QString strcur = qvariant_cast(itemData(currentIndex(), Qt::DisplayRole));
    //emit commitData(editor);
    //connect(this, SIGNAL(currentTextChanged(const QString &)),
            //this, SLOT(commitAndCloseEditor(const QString &)));
    //qDebug()< return strcur;
}


void ColorListEditor::setComboStr(QString c)
{
setCurrentIndex(findData(c, int(Qt::DisplayRole)));
}


//! [0]
//QColor ColorListEditor::getcolor() const
//{
//    return qvariant_cast(itemData(currentIndex(), Qt::DecorationRole));
//}
////! [0]


////! [1]
//void ColorListEditor::setColor(QColor color)
//{
//    setCurrentIndex(findData(color, int(Qt::DecorationRole)));
//}
////! [1]


//! [2]
void ColorListEditor::populateList()
{
//    QStringList colorNames = QColor::colorNames();


//    for (int i = 0; i < colorNames.size(); ++i) {
//        QColor color(colorNames[i]);


//        insertItem(i, colorNames[i]);
//        setItemData(i, color, Qt::DecorationRole);
//    }
    myAllComboData.append("00000--xxxxx");
    myAllComboData.append("11111--xxxxx");
    myAllComboData.append("22222--xxxxx");
    myAllComboData.append("33333--xxxxx");
    myAllComboData.append("44444--xxxxx");
    myAllComboData.append("55555--xxxxx");


    for (int i = 0; i < myAllComboData.size(); ++i)
    {
        insertItem(i, myAllComboData[i]);
        setItemData(i, myAllComboData[i], Qt::DisplayRole);
    }
}
//! [2]


window.cpp:
#include


#include "window.h"
#include "colorlisteditor.h"
#include


//! [0]
Window::Window()
{
    QItemEditorFactory *factory = new QItemEditorFactory;


    QItemEditorCreatorBase *colorListCreator =
        new QStandardItemEditorCreator();


    //factory->registerEditor(QVariant::Color, colorListCreator);
    factory->registerEditor(QVariant::String, colorListCreator);


    QItemEditorFactory::setDefaultFactory(factory);


    createGUI();
}
//! [0]
void Window::openPersistentEditor(QTableWidgetItem* item)
{
    int rowt = item->row() ;
    int col = item->column() ;
    //qDebug()<     if(col==0)
    {
        table->closePersistentEditor(item);
        item->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEnabled);
        //table->closePersistentEditor(item);
    }
}
void Window::slotSelectionChanged()
{
    //table->closePersistentEditor(lastOpen,1);
}


void Window::createGUI()
{
    QList > list;
    list << QPair(tr("Alice"), QString("00000--xxxxx")) <<
            QPair(tr("Neptun"), QString("11111--xxxxx")) <<
            QPair(tr("Ferdinand"), QString("22222--xxxxx"));


    table = new QTableWidget(3, 2);
    QObject::connect(table,SIGNAL(itemDoubleClicked(QTableWidgetItem*)),
            this,SLOT(openPersistentEditor(QTableWidgetItem*)));
    //QObject::connect(table,SIGNAL(itemSelectionChanged ()),
            //this,SLOT(slotSelectionChanged()));
    table->setHorizontalHeaderLabels(QStringList() << tr("Name")
                                                   << tr("Hair Color"));
    table->setEditTriggers(QAbstractItemView::DoubleClicked);
    //table->setEditTriggers(QAbstractItemView::NoEditTriggers);
    table->verticalHeader()->setVisible(false);
    table->resize(150, 50);


    for (int i = 0; i < 3; ++i) {
        QPair pair = list.at(i);
        QTableWidgetItem *nameItem = new QTableWidgetItem(pair.first);
        QTableWidgetItem *colorItem = new QTableWidgetItem;
        colorItem->setData(Qt::DisplayRole, pair.second);
        table->setItem(i, 0, nameItem);
        table->setItem(i, 1, colorItem);
    }
    table->resizeColumnToContents(0);
    table->horizontalHeader()->setStretchLastSection(true);


    QGridLayout *layout = new QGridLayout;
    layout->addWidget(table, 0, 0);


    QPushButton* pbn = new QPushButton("ok");


    connect(pbn, SIGNAL(clicked()),this, SLOT(oxkslot()));
    layout->addWidget(pbn,1, 0);
    setLayout(layout);
    setWindowTitle(tr("Color Editor Factory"));
}


void Window::oxkslot()
{
    int rowcnt = table->rowCount() ;
    int colcnt=table->colorCount();
    for(int i=0;i     {
        QTableWidgetItem * itemsv = table->item(i,1);
        QString tt=itemsv->text();
        qDebug()<

    }
}


main.cpp:
#include
#include "window.h"


int main(int argv, char **args)
{
    QApplication app(argv, args);


    Window window;
    window.show();


    return app.exec();
}
下面看另外一个QT的QTABLEVIEW中的一个单元格中插入COMBO的例子:

QT中的QTableView的使用_第2张图片QT中的QTableView的使用_第3张图片

untitled.pro:
QT       += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = untitled
TEMPLATE = app
SOURCES += main.cpp\
        mainwindow.cpp \
    delegate.cpp
HEADERS  += mainwindow.h \
    delegate.h
FORMS    += mainwindow.ui
delegate.h:
#ifndef DELEGATE_H
#define DELEGATE_H
#include


class SpinBoxDelegate : public QStyledItemDelegate
{
    Q_OBJECT
public:
    SpinBoxDelegate(QObject *parent = 0);
    QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
                          const QModelIndex &index) const Q_DECL_OVERRIDE;
    void setEditorData(QWidget *editor, const QModelIndex &index) const Q_DECL_OVERRIDE;
    void setModelData(QWidget *editor, QAbstractItemModel *model,
                      const QModelIndex &index) const Q_DECL_OVERRIDE;
    void updateEditorGeometry(QWidget *editor,
        const QStyleOptionViewItem &option, const QModelIndex &index) const Q_DECL_OVERRIDE;
    QVector vecStrListCombo;
    QVector vecStrListEdit;
};


#endif
mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H


#include
#include
#include "delegate.h"


namespace Ui {
class MainWindow;
}
struct testViewAllStruCombo
{
    QString col0;
    QStringList col1;
    QString col1UserSel;
};


class MainWindow : public QMainWindow
{
    Q_OBJECT


public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    void initModelAndDel();


    QStringList testHeads;
    QStandardItemModel *mystdmodel;
    //tableArrorRbtnModel *myisConfmodel;
    SpinBoxDelegate *myisOutDelegate;
    QVector delegateStrListCombo;
    QVector vec_testViewAllStruCombo;
    void gouZaoData();
    int rowt;
    int colt;
private slots:
    void on_pushButton_clicked();


private:
    Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
delegate.cpp:
#include "delegate.h"
#include
#include


SpinBoxDelegate::SpinBoxDelegate(QObject *parent)
    : QStyledItemDelegate(parent)
{
}
QWidget *SpinBoxDelegate::createEditor(QWidget *parent,
    const QStyleOptionViewItem & option ,
    const QModelIndex & index ) const
{
    int r=index.row();
    QString value = index.model()->data(index, Qt::EditRole).toString();
    QStringList valuel = index.model()->data(index, Qt::UserRole).toStringList();
    QComboBox *editor = new QComboBox(parent);
    //editor->addItems(vecStrListCombo.at(r));
    editor->addItems(valuel);
    editor->setCurrentText(value);


    editor->installEventFilter(const_cast(this));
    editor->setFrame(false);
    return editor;
}
void SpinBoxDelegate::setEditorData(QWidget *editor,
                                    const QModelIndex &index) const
{
   // int value = index.model()->data(index, Qt::EditRole).toInt();
    QString value = index.model()->data(index, Qt::EditRole).toString();
    QComboBox *spinBox = static_cast(editor);
    spinBox->setCurrentText(value);
}
void SpinBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
                                   const QModelIndex &index) const
{
    QComboBox *spinBox = static_cast(editor);
    QString value=spinBox->currentText();
    model->setData(index, value, Qt::EditRole);
}
void SpinBoxDelegate::updateEditorGeometry(QWidget *editor,
    const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
{
    editor->setGeometry(option.rect);
}
mainwindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    gouZaoData();
    initModelAndDel();
    //initModelAndDel();
}


MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::gouZaoData()
{
    rowt=3;
    colt=2;
    testHeads<<"testcol0name"<<"testcol1name";
    QStringList s1; s1<<"aaa000s1"<<"aaa111s1"<<"aaa222s1";
    QStringList s2; s2<<"aaa000s2"<<"aaa111s2"<<"aaa222s2"<<"aaa333s2";
    QStringList s3; s3<<"aaa000s3"<<"aaa111s3"<<"aaa222s3"<<"aaa333s3"<<"aaa444s3";


    testViewAllStruCombo tempCombo0;
    tempCombo0.col0="testcol0";
    tempCombo0.col1=s1;


    testViewAllStruCombo tempCombo1;
    tempCombo1.col0="testcol1";
    tempCombo1.col1=s2;


    testViewAllStruCombo tempCombo2;
    tempCombo2.col0="testcol2";
    tempCombo2.col1=s3;


    vec_testViewAllStruCombo.append(tempCombo0);
    vec_testViewAllStruCombo.append(tempCombo1);
    vec_testViewAllStruCombo.append(tempCombo2);


    delegateStrListCombo.append(s1);
    delegateStrListCombo.append(s2);
    delegateStrListCombo.append(s3);
}


void MainWindow::initModelAndDel()
{
    //QStandardItemModel *mystdmodel;
    mystdmodel=new QStandardItemModel(rowt,colt);
    mystdmodel->setHorizontalHeaderLabels(testHeads);
    ui->tableView->setModel(mystdmodel);


    myisOutDelegate = new SpinBoxDelegate(this);
    //myisOutDelegate->vecStrListCombo=delegateStrListCombo;//! 透传数据的时候,可能通过此变量,直接透传,也可以通过下面的Qt::UserRole来透传,见QWidget *SpinBoxDelegate::createEditor中的代码说明注释


    ui->tableView->setItemDelegateForColumn(colt-1,myisOutDelegate);
    ui->tableView->horizontalHeader()->setStretchLastSection(true);
    ui->tableView->resizeColumnsToContents();
    //myisConfmodel=new QStandardItemModel(this);
    for (int row = 0; row < rowt; ++row)
    {
        QStringList ComboData=delegateStrListCombo.at(row);
        //qDebug()<         testViewAllStruCombo allRowData = vec_testViewAllStruCombo.at(row);
        for (int column = 0; column < colt; ++column)
        {
            if(column==0)//0
            {
                QModelIndex index = mystdmodel->index(row, column, QModelIndex());
                mystdmodel->setData(index, QVariant(allRowData.col0),Qt::EditRole);
            }
            if(column==1)
            {
                QString tempCombo =  ComboData.at(0);
                QModelIndex index = mystdmodel->index(row, column, QModelIndex());
                mystdmodel->setData(index, QVariant(tempCombo),Qt::EditRole);
                mystdmodel->setData(index, QVariant(ComboData),Qt::UserRole);
            }
        }
    }
}


void MainWindow::on_pushButton_clicked()
{
    //! 从界面中取数据 s2为取到的值
    int j=colt-1;
        for(int i=0;i         {
            QModelIndex tt =mystdmodel->index(i,j);
            QString s2 = tt.data(Qt::EditRole).toString();
            vec_testViewAllStruCombo[i].col1UserSel=s2;
            //QString s1 = tt.data(Qt::DisplayRole).toString();//s1==s2
            //qDebug()<<"xxxxxx"<         }
}
main.cpp:
#include "mainwindow.h"
#include


int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();


    return a.exec();
}
需要代理为何种WIDGET,只需要在

SpinBoxDelegate
中写就可以了。


下面用QTABLEWIDGET来实现:

QT中的QTableView的使用_第4张图片

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include 
#include 

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    initTableWidget();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::initTableWidget()
{
    QComboBox *pcom0=new QComboBox(this);
    pcom0->addItem("1111");
    pcom0->addItem("2222");
    pcom0->addItem("3333");pcom0->setCurrentIndex(1);

    QComboBox *pcom1=new QComboBox(this);
    pcom1->addItem("1111");
    pcom1->addItem("2222");
    pcom1->addItem("3333");pcom1->setCurrentIndex(1);

    QComboBox *pcom2=new QComboBox(this);
    pcom2->addItem("1111");
    pcom2->addItem("2222");
    pcom2->addItem("3333");pcom2->setCurrentIndex(1);

    tableWidgetRowCount=3;
    tableWidgetColumnCount=2;

    ui->tableWidget->setRowCount(tableWidgetRowCount);
    ui->tableWidget->setColumnCount(tableWidgetColumnCount);

    for(int i=0;itableWidget->setItem(i, 0, newItem);
            }
        }
    ui->tableWidget->setCellWidget(0,1,pcom0);
    ui->tableWidget->setCellWidget(1,1,pcom1);
    ui->tableWidget->setCellWidget(2,1,pcom2);
}

void MainWindow::on_pushButton_clicked()
{
    QComboBox *spinBox0 = static_cast(ui->tableWidget->cellWidget(0,1));
    QString s01 = spinBox0->currentText();

    QComboBox *spinBox1 = static_cast(ui->tableWidget->cellWidget(1,1));
    QString s11 = spinBox1->currentText();

    QComboBox *spinBox2 = static_cast(ui->tableWidget->cellWidget(2,1));
    QString s21 = spinBox2->currentText();

    qDebug()<
QT中的QTableView的使用_第5张图片

QT中的QTableView的使用_第6张图片

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include 
#include 
#include "testdlg.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    initTableWidget();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::initTableWidget()
{
    /*QComboBox *pcom0=new QComboBox(this);
    pcom0->addItem("1111");
    pcom0->addItem("2222");
    pcom0->addItem("3333");pcom0->setCurrentIndex(1);

    QComboBox *pcom1=new QComboBox(this);
    pcom1->addItem("1111");
    pcom1->addItem("2222");
    pcom1->addItem("3333");pcom1->setCurrentIndex(1);

    QComboBox *pcom2=new QComboBox(this);
    pcom2->addItem("1111");
    pcom2->addItem("2222");
    pcom2->addItem("3333");pcom2->setCurrentIndex(1);*/

    tableWidgetRowCount=3;
    tableWidgetColumnCount=2;

    ui->tableWidget->setRowCount(tableWidgetRowCount);
    ui->tableWidget->setColumnCount(tableWidgetColumnCount);

    for(int i=0;itableWidget->setItem(i, 0, newItem);
            }
        }
    testdlg *plg0=new testdlg(this);
    QStringList sList0;sList0<<"true"<<"true"<<"false"<<"false";
    plg0->initAllCk(sList0);
    ui->tableWidget->setCellWidget(0,1,plg0);
    //ui->tableWidget->setCellWidget(1,1,pcom1);
    //ui->tableWidget->setCellWidget(2,1,pcom2);
}

void MainWindow::on_pushButton_clicked()
{
    /*QComboBox *spinBox0 = static_cast(ui->tableWidget->cellWidget(0,1));
    QString s01 = spinBox0->currentText();

    QComboBox *spinBox1 = static_cast(ui->tableWidget->cellWidget(1,1));
    QString s11 = spinBox1->currentText();

    QComboBox *spinBox2 = static_cast(ui->tableWidget->cellWidget(2,1));
    QString s21 = spinBox2->currentText();*/
    testdlg *spinBox0 = static_cast(ui->tableWidget->cellWidget(0,1));

    qDebug()<m_pageRes.checkBox->checkState()<m_pageRes.checkBox_2->checkState()<m_pageRes.checkBox_3->checkState()<m_pageRes.checkBox_4->checkState();
}

#ifndef TESTDLG_H
#define TESTDLG_H

#include >
#include 
#include "ui_testdlg.h"

class testdlg : public QWidget
{
    Q_OBJECT

public:
    testdlg(QWidget *parent=0);
    ~testdlg();
    void initAllCk(QStringList valuel);
    Ui::testDlg m_pageRes;
};

#endif

#include 

#include "testdlg.h"
//#include "TableItemComboBoxEditor.h"
#include 
#include 

//! [0]
testdlg::testdlg(QWidget *parent)
{
    m_pageRes.setupUi(this);
    this->setWindowFlags(Qt::FramelessWindowHint);
}

testdlg::~testdlg()
{

}

void testdlg::initAllCk(QStringList valuel)
{
    if(valuel.at(0)==tr("false")) m_pageRes.checkBox->setCheckState(Qt::Unchecked);
    if(valuel.at(0)==tr("true")) m_pageRes.checkBox->setCheckState(Qt::Checked);

    if(valuel.at(1)==tr("false")) m_pageRes.checkBox_2->setCheckState(Qt::Unchecked);
    if(valuel.at(1)==tr("true")) m_pageRes.checkBox_2->setCheckState(Qt::Checked);

    if(valuel.at(2)==tr("false")) m_pageRes.checkBox_3->setCheckState(Qt::Unchecked);
    if(valuel.at(2)==tr("true")) m_pageRes.checkBox_3->setCheckState(Qt::Checked);

    if(valuel.at(3)==tr("false")) m_pageRes.checkBox_4->setCheckState(Qt::Unchecked);
    if(valuel.at(3)==tr("true")) m_pageRes.checkBox_4->setCheckState(Qt::Checked);
}


你可能感兴趣的:(QT学习)