Qt 笔记(1) 为QTableWidget设置自定义代理

6 篇文章 1 订阅

参考Qt 例程"Spin Box Delegate Example", 自己实现了 QSpinBoxDelegate, QDoubleSpinBoxDelegate, QComboBoxDelegate 和 QDateEditDelegate四个类, 分别用于为QTableWidget 的单元格添加QSpinBox, QDoubleSpinBox, QComboBox 和 QDateEdit代理. 代码可从我的资源myTable.zip下载. 效果如下图所示:

watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2ZhbHdhdA==,size_16,color_FFFFFF,t_70

watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2ZhbHdhdA==,size_16,color_FFFFFF,t_70

watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2ZhbHdhdA==,size_16,color_FFFFFF,t_70

watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2ZhbHdhdA==,size_16,color_FFFFFF,t_70

关于这几个delegate类的使用参见 mainwindow.cpp 中的 tableWidgetInit() 函数: 

void MainWindow::tableWidgetInit(QTableWidget *table)
{
    // 设置选中整行
    table->setSelectionBehavior(QAbstractItemView::SelectRows);
    // 设置选中单行
    table->setSelectionMode(QAbstractItemView::SingleSelection);

    // 设置表头
    QStringList headers;
    headers << tr("ID") << tr("Name") << tr("Gender") << tr("Age") << tr("Weight") << tr("Birthday");
    table->setColumnCount(headers.count());
    table->setHorizontalHeaderLabels(headers);

    QHeaderView *headerView = table->horizontalHeader();
    headerView->setSectionResizeMode(QHeaderView::Stretch);

    gender << tr("Male") << tr("Female");
    QComboBoxDelegate * genderDelegate = new QComboBoxDelegate(this);
    genderDelegate->addItems(gender);
    table->setItemDelegateForColumn(2, genderDelegate);

    QSpinBoxDelegate *ageDelegate = new QSpinBoxDelegate(this);
    ageDelegate->setMinimum(0);
    ageDelegate->setMaximum(200);
    table->setItemDelegateForColumn(3, ageDelegate);

    QDoubleSpinBoxDelegate *weightDelegate = new QDoubleSpinBoxDelegate();
    weightDelegate->setMinimum(1);
    weightDelegate->setMaximum(300);
    weightDelegate->setDecimals(1);
    table->setItemDelegateForColumn(4, weightDelegate);

    QDateEditDelegate *birthdayDelegate = new QDateEditDelegate;
    table->setItemDelegateForColumn(5, birthdayDelegate);

}

其中, QComboBoxDelegate 中的下拉列表项可以自由添加, QSpinBoxDelegate 可以设置最小值和最大值, QDoubleSpinBoxDelegate 可以设置最小值, 最大值和小数位数. 

 
 
 
 

  • 0
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
Qt中,可以通过以下步骤在QTableWidget设置右击出现菜单: 1. 创建一个QMenu对象,该对象用于显示右击菜单的内容。 2. 为QTableWidget对象设置一个contextMenuPolicy,该策略指示在右击时显示菜单。可以将其设置Qt::CustomContextMenu。 3. 通过connect函数将customContextMenuRequested信号连接到一个槽函数。 4. 在槽函数中使用QCursor的pos方法获取鼠标的当前位置,然后使用exec方法在该位置显示菜单。 以下是一个简单的示例代码: ```cpp #include <QApplication> #include <QTableWidget> #include <QMenu> #include <QContextMenuEvent> class MyTableWidget : public QTableWidget { public: MyTableWidget(QWidget* parent = nullptr) : QTableWidget(parent) { setContextMenuPolicy(Qt::CustomContextMenu); connect(this, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(showContextMenu(QPoint))); } protected: void showContextMenu(const QPoint& pos) { QMenu menu(this); QAction* action1 = menu.addAction("菜单项1"); QAction* action2 = menu.addAction("菜单项2"); // 在这里可以根据需要添加更多菜单项 QAction* selectedItem = menu.exec(mapToGlobal(pos)); if (selectedItem == action1) { // 执行菜单项1的操作 } else if (selectedItem == action2) { // 执行菜单项2的操作 } } }; int main(int argc, char *argv[]) { QApplication a(argc, argv); MyTableWidget tableWidget; tableWidget.setColumnCount(3); tableWidget.setRowCount(3); // 在这里可以设置表格的内容 tableWidget.show(); return a.exec(); } ``` 在这个示例中,我们创建了一个自定义QTableWidget子类MyTableWidget,并在构造函数中设置了contextMenuPolicy以及连接了customContextMenuRequested信号。在槽函数showContextMenu中,我们创建了一个QMenu对象,并添加了两个菜单项。然后使用exec方法在鼠标位置显示菜单。根据选中的菜单项,可以执行相应的操作。 另外,为了确保在右击时能够获取到正确的鼠标位置,我们在showContextMenu函数的mapToGlobal方法中传递了pos参数,该参数经过mapToGlobal转换成了全局坐标。这样,菜单将在正确的位置显示。 希望对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

falwat

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值