• 9623阅读
  • 5回复

如何实现QTableView的单元格被点击之后其内容属于被选中状态(已解决,谢谢关注) [复制链接]

上一主题 下一主题
离线tangqw
 
只看楼主 倒序阅读 楼主  发表于: 2012-06-11
关键词: QTableView
现在项目里想搞个UI界面,我想在点击单元格的时候单元格的内容属于被选中的状态,苦于接触UI的时间太少很多东西都不解,向各位达人求救
bool PluginModel::setData( const QModelIndex &index, const QVariant &value, int role )
{
    if (index.isValid()    && role == Qt::EditRole && value.toString().toStdString()!="")
    {
        switch(index.column()){
        case 0:
            break;
            break;
        case 1:
            pluginObjects[index.row()].set_name(value.toString().toStdString());
            break;
        case 2:
            pluginObjects[index.row()].set_description(value.toString().toStdString());
            break;
        case 3:
            pluginObjects[index.row()].set_platform(value.toString().toStdString());
            break;
        case 4:
            pluginObjects[index.row()].set_version(value.toString().toStdString());
            break;
        case 5:
            pluginObjects[index.row()].set_modidate(value.toString().toStdString());
            break;
        }
        emit dataChanged(index, index);    
        return true;
    }
    return false;
}
Qt::ItemFlags PluginModel::flags( const QModelIndex &index ) const
{
    Qt::ItemFlags flags = QAbstractItemModel::flags(index);
    flags |= Qt::ItemIsEditable;
    return flags;
}

这样点击单元格之后单元格为一个空的单元格,我是想点击的时候还显示原来的内容,然后再对原来的内容进行修改,我应该怎么实现呢?
先谢谢各位啦!!
离线tangxunmin

只看该作者 1楼 发表于: 2012-06-11
回 楼主(tangqw) 的帖子
解决了,可以把解决方案贴出来下。不要发完贴 就来句谢谢关注
离线passion_wu
只看该作者 2楼 发表于: 2012-06-11
需要自己写一个类继承自QItemDelegate,然后调用
void QAbstractItemView::setItemDelegate ( QAbstractItemDelegate * delegate )
这是我写的delegate类,希望对你有帮助,头文件你应该知道怎么写。

/*
* \brief 构造函数
*/
CptDelegate::CptDelegate(QObject *parent) :
    QItemDelegate(parent) {
}

/*
* \brief 创建新的编辑器
*/
QWidget* CptDelegate::createEditor(QWidget *parent,
        const QStyleOptionViewItem &option, const QModelIndex &index) const {
    QLineEdit *editor = new QLineEdit(parent);
    return editor;
}

/*
* \brief 打开编辑器时显示原来的内容
*/
void CptDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const {
    QString strVal = index.model()->data(index, Qt::EditRole).toString();
    QLineEdit *inputDialog = qobject_cast<QLineEdit*> (editor);
    inputDialog->setText(strVal);
}

/*
* \brief 确认编辑完时将内容写入model
*/
void CptDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
        const QModelIndex &index) const {
    QString strVal = qobject_cast<QLineEdit*> (editor)->text();
    model->setData(index, strVal, Qt::EditRole); //需要重写model的setData函数
}

/*
* \brief 定位输入框的位置
*/
void CptDelegate::updateEditorGeometry(QWidget * editor,
        const QStyleOptionViewItem & option, const QModelIndex & index) const {
    editor->setGeometry(option.rect);
}
离线tangqw
只看该作者 3楼 发表于: 2012-06-11
回 1楼(tangxunmin) 的帖子
,以后注意,以后注意。
我之前data函数里显示文本的时候采用了Qt::Display方式,这样的话就导致了这个问题的出现,后来我干脆把它去掉,结果呢文本的内容是可以被选中,没办法了,最后用Qt::Display || Qt::Editrole,结果成功了,哈哈。
QVariant PluginModel::data( const QModelIndex &index, int role ) const
{
    if (!index.isValid())
        return QVariant();

    if (role == Qt::TextAlignmentRole)
    {
        return int(Qt::AlignRight | Qt::AlignVCenter);
    }
   else if (role == Qt::DisplayRole  || role == Qt::EditRole)  //就是这个地方
   {
        switch ( index.column() )
        {
            case 0: return QString::fromStdString(pluginObjects.at(index.row()).uuid());
            case 1: return QString::fromStdString(pluginObjects.at(index.row()).name());
            case 2: return QString::fromStdString(pluginObjects.at(index.row()).description());
            case 3: return pluginObjects.at(index.row()).platform();
            case 4: return QString::fromStdString(pluginObjects.at(index.row()).version());
            case 5: return QString::fromStdString(pluginObjects.at(index.row()).modidate());
        }
    }

    return QVariant();
}
离线tangqw
只看该作者 4楼 发表于: 2012-06-11
回 2楼(passion_wu) 的帖子
谢谢兄台,你的方法也是不错,现在我学着用委托来改善项目,你的例子给我很多的参考价值
离线passion_wu
只看该作者 5楼 发表于: 2012-06-11
呵呵,你的委托是指?
我的几个函数都是委托的例子啊。
快速回复
限100 字节
 
上一个 下一个