【问题标题】:Qt QTableView implementing toggle button and checkbox delegatesQt QTableView 实现切换按钮和复选框委托
【发布时间】:2018-04-26 10:22:07
【问题描述】:

我目前正在尝试通过子类化 QTableViewQAbstractTableModelQStyledItemDelegate 来构建 Qt 表,以维护 MVC 架构的一些外观。

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

http://qt-project.org/doc/qt-4.8/modelview.html

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

我注意到模型的数据方法可用于实现复选框,但我需要为按钮自定义委托,因此我也将其用于复选框。

无论如何,我无法在 Internet 上找到任何通过使用 QTableView 对象和文本、复选框和按钮的混合创建表格的体面示例。有哪位好先生能给我指出正确的方向吗?

【问题讨论】:

  • 我已经在上面说过我正在创建一个委托。提供一些示例或主题的新内容或不回答。创建委托并覆盖单元格内容是给定的。否则我会使用默认委托。使用按钮或复选框的示例是必需的。
  • 所以您希望我们为您编写代码?没有你的任何尝试?
  • 使用相同的代码,但绘制一个按钮而不是复选框:stackoverflow.com/a/16301316/1035613
  • 不,我期待的可能是一个页面链接,该页面有一个委托示例,使用它的 paint() 方法创建一个切换按钮或一个复选框,如我声明的最后一段所示。考虑按钮或复选框的绘制方法应该可以工作或不工作 如果我有一个示例,我需要请任何人指出我进一步示例的方向,这不太可能。无论如何,很明显这是人们以前做过的事情(因此会知道如何做,或者会有一个如何做的例子)。
  • 非常感谢,这就是我所需要的。

标签: c++ qt model-view-controller


【解决方案1】:

您不需要自定义委托来在表格视图中拥有复选框和切换按钮。您可以简单地使您的项目可检查并将其设置为您的模型,例如:

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);

【讨论】:

    【解决方案2】:

    根据上面 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);
    
    }   
    }
    

    【讨论】:

    • 顺便说一句,您可以使用setItemDelegateForColumn便捷方法,而不是检查委托中的列号。
    猜你喜欢
    • 2014-07-11
    • 1970-01-01
    • 2015-11-28
    • 2018-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多