| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1323 人关注过本帖
标题:Qt 中使用代理,但是不起作用
只看楼主 加入收藏
freebigfish
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2014-5-12
结帖率:0
收藏
已结贴  问题点数:10 回复次数:1 
Qt 中使用代理,但是不起作用
各位大神,我在tabview中使用一个代理,但是没起作用,就像没使用代理一样,下面是我的代码。
//创建界面的代码
   viewGroup = new QGroupBox;
    viewGroup->setFixedHeight( 350 );
    view = new QTableView( viewGroup );
    QStringList list;
    list<<tr( "线路1" )<<tr( "状态1" )<<tr( "状态2" )<<tr( "结论" );
    viewModel = new QStandardItemModel( 0, list.count( ), this );
//    viewModel = new CustomModel(0,list.count(),this);
    view->setModel( viewModel );
    SexDelegate comboBoxDelegate1;
//    view->setItemDelegate(&comboBoxDelegate);
    view->setItemDelegateForColumn(1, &comboBoxDelegate1);
    SexDelegate comboBoxDelegate2;
    view->setItemDelegateForColumn(2, &comboBoxDelegate2);
    SexDelegate comboBoxDelegate3;
    view->setItemDelegateForColumn(3, &comboBoxDelegate3);
    SexDelegate comboBoxDelegate0;
    view->setItemDelegateForColumn(0, &comboBoxDelegate0);

    view->setMinimumSize( 665, 350 );
    view->horizontalHeader()->setStretchLastSection( true );
    view->horizontalHeader()->setDefaultAlignment( Qt::AlignCenter );
    viewModel->setHorizontalHeaderLabels( list );
    view->verticalHeader( )->setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
    view->verticalHeader( )->setEnabled( false );
//    view->setEditTriggers( QAbstractItemView::NoEditTriggers );   //设置为不可编辑2
    view->setSelectionBehavior( QAbstractItemView::SelectRows );
    view->setSelectionMode( QAbstractItemView::SingleSelection );

我的代理代码:
class StateDelegate : 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("Open");
        editor->addItem("Close");
        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);
    }
};
2014-05-12 17:00
powerwheel
Rank: 2
等 级:论坛游民
帖 子:4
专家分:25
注 册:2010-9-5
收藏
得分:10 
能有用嘛,你的对象都是在栈上申请的,作用范围就在你的初始化函数里面
应该是这样弄的:
view->setItemDelegateForColumn(1, new SexDelegate());
2014-05-13 18:11
快速回复:Qt 中使用代理,但是不起作用
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017400 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved