我怎样才能获得QTableWidget的委托?

时间:2016-08-26 07:31:37

标签: c++ delegates qtablewidget qmouseevent

在QTableWidget上获取委托的问题,我正在尝试使用两个委托(comboboxdelegate,spinbox委托)来控制具有2列和一行的QTableWidget中的用户输入,但是在阅读了委托类页面并查看了SpinBox之后&安培; comboox代表的例子,我以为我会弄清楚事情是如何运作的,但显然没有。

(对不起我的英文),感谢任何帮助。

代码如下。

notepad.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

notepad.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();
}

1 个答案:

答案 0 :(得分:0)

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

MyCustomDelegate * theDelegate = ( MyCustomDelegate *) myTableWidget->itemDelegate();