Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/141.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ Qt QTableView实现切换按钮和复选框委托_C++_Qt_Model View Controller - Fatal编程技术网

C++ Qt QTableView实现切换按钮和复选框委托

C++ Qt QTableView实现切换按钮和复选框委托,c++,qt,model-view-controller,C++,Qt,Model View Controller,我目前正试图通过子类化QTableView、QAbstractTableModel和QStyledItemDelegate来构建一个Qt表,以维护MVC体系结构的一些外观 我使用这个简单的示例作为构建的基础,因为我以前没有接触过Qt表类: 无论如何,该表主要是文本列,但它也需要一个切换按钮列和一个复选框列 我注意到模型的data方法可以用来实现一个复选框,但是我需要一个按钮的自定义委托,所以我也将它用于复选框 无论如何,我在互联网上找不到任何像样的例子,可以通过混合使用文本、复选框和按钮的QT

我目前正试图通过子类化
QTableView
QAbstractTableModel
QStyledItemDelegate
来构建一个Qt表,以维护MVC体系结构的一些外观

我使用这个简单的示例作为构建的基础,因为我以前没有接触过Qt表类:

无论如何,该表主要是文本列,但它也需要一个切换按钮列和一个复选框列

我注意到模型的data方法可以用来实现一个复选框,但是我需要一个按钮的自定义委托,所以我也将它用于复选框


无论如何,我在互联网上找不到任何像样的例子,可以通过混合使用文本、复选框和按钮的
QTableView
对象来创建表。你们中任何一位优秀的SIR都能为我指出正确的方向吗?

在tableview中设置复选框和切换按钮不需要自定义代理。您只需将项目设置为可检查,并将其设置为您的模型,如:

QStandardItem *item = new QStandardItem( true );
item->setCheckable(true);
item->setCheckState(Qt::Unchecked);

QStandardItemModel * model = new QStandardItemModel( 0, 2 );
model->setRowCount(1);
model->setItem(0, 0, item);
对于切换按钮,您可以执行以下操作:

QPushButton * but = new QPushButton(this);
but->setCheckable(true);
but->setText("Toggle");

ui->tableView->setIndexWidget(model->item(0,1)->index(),but);

基于上面提供的信息Dmitry,我在我的代理中实现了以下绘制方法,用于呈现我的按钮和复选框。显然,这需要一个editorEvent()来做任何我可以添加的事情,如果它被证明有用的话

void DDUTableDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option,
                            const QModelIndex& index) const
{
int col = index.column();

if (col == VIEW_COLUMN) {
    // Draw our checkbox indicator
    bool value = index.data(Qt::EditRole).toBool();
    QStyleOptionButton checkbox_indicator;

    // Set our button state to enabled
    checkbox_indicator.state |= QStyle::State_Enabled;
    checkbox_indicator.state |= (value) ? QStyle::State_On : QStyle::State_Off;

    // Get our deimensions
    checkbox_indicator.rect = QApplication::style()->subElementRect( QStyle::SE_CheckBoxIndicator, &checkbox_indicator, NULL );

    // Position our indicator
    const int x = option.rect.center().x() - checkbox_indicator.rect.width() / 2;
    const int y = option.rect.center().y() - checkbox_indicator.rect.height() / 2;

    checkbox_indicator.rect.moveTo( x, y );

    if (option.state & QStyle::State_Selected) {
        painter->fillRect(option.rect, option.palette.highlight());       
    }

    QApplication::style()->drawControl( QStyle::CE_CheckBox, &checkbox_indicator, painter );
}
else if (col == TEST_COLUMN) {
     bool value = index.data(Qt::EditRole).toBool();

     QStyleOptionButton button;

     // Set our button to fill the entire cell contents
     button.rect = option.rect;

     // Set our button state to enabled
     button.state |= QStyle::State_Enabled;

     if (value) {
         button.state |= QStyle::State_Sunken;
         button.text = STOP_TEST;
     }
     else {
          button.text = START_TEST;
     }

     if (option.state & QStyle::State_Selected) {
        painter->fillRect(option.rect, option.palette.highlight());         
     }

     QApplication::style()->drawControl(QStyle::CE_PushButton, &button, painter);

}   
}

我已经在上面说过,我正在创建一个代理。提供一些新的例子或线索,否则不要回答。创建委托并重写单元格内容是一个给定的过程。否则我将使用默认的委托。使用按钮或复选框的示例是必需的。那么您希望我们为您编写代码吗?没有您方面的任何尝试?使用相同的代码,但绘制一个按钮而不是复选框:不,我希望可能会有一个链接,指向一个页面,该页面有一个代理使用其paint()方法创建切换按钮或复选框的示例,如我的语句的最后一段所示。考虑到按钮或复选框的绘制方法应该要么起作用,要么不起作用。如果我有一个示例,我需要请任何人为我指出进一步示例的方向,这将是不可能的。无论如何,这显然是人们以前做过的事情(因此会知道如何做,或者会有一个如何做的例子)。非常感谢,这就是我所需要的。顺便说一句,你可以使用
setItemDelegateForColumn
便利方法,而不是检查委托中的列号。