C++ qt 自带的日历控件设置属性让年份变为下拉表

QCalendarWidget *calendar = new QCalendarWidget();
calendar->setGridVisible(true);
calendar->setHorizontalHeaderFormat(QCalendarWidget::LongDayNames);
calendar->setProperty("yearRange", QVariant(QString("1900:2100")));
calendar->setProperty("navigationBarVisible", QVariant(false));
calendar->setProperty("yearShown", QVariant(true));

在上面的代码中,我们设置了QCalendarWidget的属性yearRange、navigationBarVisible和yearShown。yearRange属性指定了可选的年份范围,navigationBarVisible属性指定了是否显示导航栏,yearShown属性指定了是否显示下拉列表选择年份。 通过设置这些属性,我们可以轻松地将年份变为下拉列表。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Qt 中,可以通过自定义 QHeaderView 来实现在头添加控件的功能。具体步骤如下: 1. 定义一个继承自 QHeaderView 的子类,例如 MyHeaderView。 2. 在 MyHeaderView 中重写 paintSection() 函数,该函数会在绘制头时被调用。 3. 在 paintSection() 函数中,首先调用父类的 paintSection() 函数,然后在需要添加控件的位置进行控件的绘制。 以下是一个添加 QPushButton 控件的例子: ```cpp class MyHeaderView : public QHeaderView { public: MyHeaderView(Qt::Orientation orientation, QWidget *parent = nullptr) : QHeaderView(orientation, parent) { } protected: void paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const override { // 调用父类的 paintSection() 函数 QHeaderView::paintSection(painter, rect, logicalIndex); // 在第一列头的右侧绘制一个 QPushButton if (logicalIndex == 0) { QPushButton button("Button", const_cast<QWidget*>(static_cast<const QWidget*>(parent()))); QRect buttonRect = QRect(rect.right() - 20, rect.top() + 2, 18, 18); // 控件的位置和大小 button.setGeometry(buttonRect); button.show(); } } }; ``` 使用时,将格的水平头和垂直头分别设置为 MyHeaderView 类的实例即可: ```cpp QTableWidget table; table.setHorizontalHeader(new MyHeaderView(Qt::Horizontal, &table)); table.setVerticalHeader(new MyHeaderView(Qt::Vertical, &table)); ``` 注意:由于 QPushButton 控件是在 paintSection() 函数中创建的,因此需要使用 const_cast 和 static_cast 将 parent 指针转换为非 const 类型。同时,由于 QPushButton 控件是在 MyHeaderView 的生命周期内创建的,因此需要在 paintSection() 函数中设置其位置和大小,并在 paintSection() 函数之外手动调用其 show() 函数显示控件

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值