0

在 QTableWidget 上获取代表时出现问题,我正在尝试使用两个代表(comboboxdelegate、spinbox 代表)来控制具有 2 列和 1 行的 QTableWidget 中的用户输入,但是在阅读了 Delegate Classes 页面并查看了 SpinBox & comboox Delegate例如,我以为我已经弄清楚事情是如何运作的,但显然还没有。

(对不起我的英语),任何帮助表示赞赏。

代码如下。

记事本.h 文件

#ifndef NOTEPAD_H
#define NOTEPAD_H
#include <QtGui>

class Notepad : public QMainWindow
{
    Q_OBJECT

   public:
        //Notepad(QWidget *parent = 0, Qt::WFlags flags = 0);
        Notepad();
        void test();
        QTableWidget* table ;
    public slots:
        void add();
        void Delete();
        void mouseReleaseEvent ( QMouseEvent * event );

    private:
        QAction *add_action;
        QAction *Delete_action;

};

class SpinBoxDelegate : public QStyledItemDelegate,public Notepad
{
    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*/;
};

class ComboBoxDelegate : public QStyledItemDelegate,public Notepad
{
Q_OBJECT

public:
ComboBoxDelegate(QObject *parent = 0);

QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
const QModelIndex &index) const;

void setEditorData(QWidget *editor, const QModelIndex &index) const;
void setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const;

void updateEditorGeometry(QWidget *editor,
const QStyleOptionViewItem &option, const QModelIndex &index) const;
};

#endif // NOTEPAD_H

记事本.cpp 文件

#include "notepad.h"
#include <QMessageBox>
#include <QTableView>
#include <QMouseEvent>
#include <QSpinBox>
#include <QComboBox>
#include <QStandardItemModel>
Notepad::Notepad()
{
    table = new QTableWidget();
     test() ;
        SpinBoxDelegate();
        ComboBoxDelegate();
    add_action = new QAction(tr("Add cell"), this);
    add_action->setIcon(QIcon("add.jpg"));
    Delete_action = new QAction(tr("Delete cell"), this);
    Delete_action->setIcon(QIcon("delete.jpg"));

    connect(Delete_action, SIGNAL(triggered()), this, SLOT(Delete()));
    connect(add_action, SIGNAL(triggered()), this, SLOT(add()));

    //tableItem->setFlags(tableItem->flags() ^ Qt::ItemIsEditable);

    centralWidget()->setAttribute(Qt::WA_TransparentForMouseEvents);
    centralWidget()->setAttribute(Qt::WA_MouseTracking,true);

    setMouseTracking(true);

}
void Notepad::test() 
{       

    QTableWidgetItem* tableItem = new QTableWidgetItem();

    table->setRowCount(1);
    table->setColumnCount(3);
    table->setItem(0,0,new QTableWidgetItem());
    table->setItem(0,1,new QTableWidgetItem());
    table->setItem(0,2,new QTableWidgetItem());

    table->setMouseTracking(true);
    table->viewport()->setMouseTracking(true);
    table->installEventFilter(this);
    table->viewport()->installEventFilter(this);

    table->setSelectionBehavior(QAbstractItemView::SelectRows);
    table->setSelectionMode(QAbstractItemView::SingleSelection);

    QStandardItemModel model((table->rowCount()),(table->columnCount()));
    SpinBoxDelegate sdelegate;

    ComboBoxDelegate comDel;
     for (int row = 0; row < 4; ++row)
    {
        QWidget *parent;
        QStyleOptionViewItem option;
        for (int column = 0; column < 1; ++column)
        {

            table->setItemDelegateForColumn( column, &comDel);
            QModelIndex index = model.index(row, column, QModelIndex());
            model.setData(index, QVariant((row + 1) * (column + 1)));
        }
        for (int column = 1; column < 2; ++column) 
        {

        table->setItemDelegateForColumn( column, &sdelegate  );
        QModelIndex index = model.index(row, column, QModelIndex());
        model.setData(index, QVariant((row + 1) * (column + 1)));
        }
    }




    table->setHorizontalHeaderItem(0, new QTableWidgetItem("combobox"));
    table->setHorizontalHeaderItem(1, new QTableWidgetItem("spinbox"));
    table->setHorizontalHeaderItem(2, new QTableWidgetItem("lineEdit"));
    tableItem->setFlags(tableItem->flags() ^ Qt::ItemIsEditable);
    table->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
    setCentralWidget(table);

}

void Notepad::mouseReleaseEvent (QMouseEvent * event )
{   
    QMessageBox* msgBox;
    if(event->button() == Qt::RightButton)
      {
QMouseEvent *mouseEvent = static_cast<QMouseEvent*> (event);
       QMenu *menu = new QMenu(this);
       menu->addAction(add_action);
       menu->addAction(Delete_action);
       menu->exec(mouseEvent->globalPos());
       //msgBox->setInformativeText("u pressed right button");            
    } 
}
void Notepad::add() 
{

    table->insertRow( 1);

    setCentralWidget(table);
    centralWidget()->setAttribute(Qt::WA_TransparentForMouseEvents);
    setMouseTracking(true);
}
void Notepad::Delete() 
{

    table->removeRow(1);

    setCentralWidget(table);
    centralWidget()->setAttribute(Qt::WA_TransparentForMouseEvents);
    setMouseTracking(true);
}








SpinBoxDelegate::SpinBoxDelegate(QObject *parent)
    : QStyledItemDelegate(parent)
{
}
QWidget *SpinBoxDelegate::createEditor(QWidget *parent,
    const QStyleOptionViewItem &/* option */,
    const QModelIndex &/* index */)/* const*/
{

    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*/
{
    int value = index.model()->data(index, Qt::EditRole).toInt();
    QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
    spinBox->setValue(value);
}

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

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

QWidget *ComboBoxDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem &/* option */,
const QModelIndex &/* index */) const
{
QComboBox *editor = new QComboBox(parent);
QStringList list ;
list << "srikanth" << "dilip";
editor->addItems(list);
//editor->installEventFilter(const_cast<ComboBoxDelegate*>(this));

return editor;
}

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

QComboBox *comboBox = static_cast<QComboBox*>(editor);
comboBox->addItem(value);
}

void ComboBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const
{
QComboBox *comboBox = static_cast<QComboBox*>(editor);
QString value = comboBox->currentText();

model->setData(index, value);
}

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

main.cpp 文件

#include <QtGui>
#include <notepad.h>
int main(int argv, char **args)  {
    QApplication app(argv, args);

   Notepad notepad;

    notepad.show();

    return app.exec();
}
4

1 回答 1

0

QTableWidget 具有获取委托的功能。然后,您可以将其转换为您的自定义委托,假设您确定它实际上是这种类型。

MyCustomDelegate * theDelegate = ( MyCustomDelegate *) myTableWidget->itemDelegate();
于 2016-12-16T19:17:27.440 回答