当前位置:网站首页>QSS、QDateEdit、QCalendarWidget自定义设置
QSS、QDateEdit、QCalendarWidget自定义设置
2022-04-22 05:44:00 【东方忘忧】


日期编辑器的样式只需要设置qss即可。
m_dateedit->setStyleSheet("QDateEdit#MDateEdit{background:#4455ff;color:white;selection-color:yellow;"
"font-size:24px;"
"border-radius:5px;"
"border:2px solid #4455ff;}"
"QDateEdit#MDateEdit:hover{border:2px solid black;}"
"QDateEdit#MDateEdit:focus{border:2px solid gray;}"
"QDateEdit#MDateEdit::drop-down{width:30px;background:#A055ffff;border-radius:2px;}"
"QDateEdit#MDateEdit::down-arrow{image:url(/home/yjd/Desktop/归档/菜单-悬浮.png)}")
日历显示器也可以通过qss设置样式,但是无法更改里面按钮的位置,文本,图标。
m_dateedit->setStyleSheet("QDateEdit#MDateEdit{background:#4455ff;color:white;selection-color:yellow;"
"font-size:24px;"
"border-radius:5px;"
"border:2px solid #4455ff;}"
"QDateEdit#MDateEdit:hover{border:2px solid black;}"
"QDateEdit#MDateEdit:focus{border:2px solid gray;}"
"QDateEdit#MDateEdit::drop-down{width:30px;background:#A055ffff;border-radius:2px;}"
"QDateEdit#MDateEdit::down-arrow{image:url(/home/yjd/Desktop/归档/菜单-悬浮.png)}"
"QCalendarWidget#MCalendarwidget{border-radius:8px;}"
"QCalendarWidget QTableView{alternate-background-color: rgb(128, 128, 128);"
"background-color: #AFAFAE;"
"color:white;"
"selection-background-color:#dddddd;"
"selection-color:yellow;"
"border-left: 2px solid #AFAFAE;"
"border-right: 2px solid #AFAFAE;"
"border-bottom: 2px solid #AFAFAE;"
"border-bottom-left-radius:8px;border-bottom-right-radius:8px;}");
如果想要更改内部的按钮位置,这个时候你需要了解QCalendarWidget的源码,主要以下两个函数:
QCalendarWidget::QCalendarWidget(QWidget *parent)
: QWidget(*new QCalendarWidgetPrivate, parent, 0)
{
Q_D(QCalendarWidget);
setAutoFillBackground(true);
setBackgroundRole(QPalette::Window);
QVBoxLayout *layoutV = new QVBoxLayout(this);
layoutV->setMargin(0);
d->m_model = new QCalendarModel(this);
QTextCharFormat fmt;
fmt.setForeground(QBrush(Qt::red));
d->m_model->m_dayFormats.insert(Qt::Saturday, fmt);
d->m_model->m_dayFormats.insert(Qt::Sunday, fmt);
d->m_view = new QCalendarView(this);
d->m_view->setObjectName(QLatin1String("qt_calendar_calendarview"));
d->m_view->setModel(d->m_model);
d->m_model->setView(d->m_view);
d->m_view->setSelectionBehavior(QAbstractItemView::SelectItems);
d->m_view->setSelectionMode(QAbstractItemView::SingleSelection);
d->m_view->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
d->m_view->horizontalHeader()->setSectionsClickable(false);
d->m_view->verticalHeader()->setSectionResizeMode(QHeaderView::Stretch);
d->m_view->verticalHeader()->setSectionsClickable(false);
d->m_selection = d->m_view->selectionModel();
d->createNavigationBar(this);
d->m_view->setFrameStyle(QFrame::NoFrame);
d->m_delegate = new QCalendarDelegate(d, this);
d->m_view->setItemDelegate(d->m_delegate);
d->update();
d->updateNavigationBar();
setFocusPolicy(Qt::StrongFocus);
setFocusProxy(d->m_view);
setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
connect(d->m_view, SIGNAL(showDate(QDate)),
this, SLOT(_q_slotShowDate(QDate)));
connect(d->m_view, SIGNAL(changeDate(QDate,bool)),
this, SLOT(_q_slotChangeDate(QDate,bool)));
connect(d->m_view, SIGNAL(clicked(QDate)),
this, SIGNAL(clicked(QDate)));
connect(d->m_view, SIGNAL(editingFinished()),
this, SLOT(_q_editingFinished()));
connect(d->prevMonth, SIGNAL(clicked(bool)),
this, SLOT(_q_prevMonthClicked()));
connect(d->nextMonth, SIGNAL(clicked(bool)),
this, SLOT(_q_nextMonthClicked()));
connect(d->yearButton, SIGNAL(clicked(bool)),
this, SLOT(_q_yearClicked()));
connect(d->monthMenu, SIGNAL(triggered(QAction*)),
this, SLOT(_q_monthChanged(QAction*)));
connect(d->yearEdit, SIGNAL(editingFinished()),
this, SLOT(_q_yearEditingFinished()));
layoutV->setMargin(0);
layoutV->setSpacing(0);
layoutV->addWidget(d->navBarBackground);
layoutV->addWidget(d->m_view);
d->m_navigator = new QCalendarTextNavigator(this);
setDateEditEnabled(true);
}
void QCalendarWidgetPrivate::createNavigationBar(QWidget *widget)
{
Q_Q(QCalendarWidget);
navBarBackground = new QWidget(widget);
navBarBackground->setObjectName(QLatin1String("qt_calendar_navigationbar"));
navBarBackground->setAutoFillBackground(true);
navBarBackground->setBackgroundRole(QPalette::Highlight);
prevMonth = new QPrevNextCalButton(navBarBackground);
nextMonth = new QPrevNextCalButton(navBarBackground);
prevMonth->setAutoRaise(true);
nextMonth->setAutoRaise(true);
prevMonth->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Minimum);
nextMonth->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Minimum);
nextMonth->setAutoRaise(true);
updateButtonIcons();
prevMonth->setAutoRepeat(true);
nextMonth->setAutoRepeat(true);
monthButton = new QCalToolButton(navBarBackground);
monthButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Minimum);
monthButton->setAutoRaise(true);
monthButton->setPopupMode(QToolButton::InstantPopup);
monthMenu = new QMenu(monthButton);
for (int i = 1; i <= 12; i++) {
QString monthName(q->locale().standaloneMonthName(i, QLocale::LongFormat));
QAction *act = monthMenu->addAction(monthName);
act->setData(i);
monthToAction[i] = act;
}
monthButton->setMenu(monthMenu);
yearButton = new QCalToolButton(navBarBackground);
yearButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Minimum);
yearButton->setAutoRaise(true);
yearEdit = new QSpinBox(navBarBackground);
QFont font = q->font();
font.setBold(true);
monthButton->setFont(font);
yearButton->setFont(font);
yearEdit->setFrame(false);
yearEdit->setMinimum(m_model->m_minimumDate.year());
yearEdit->setMaximum(m_model->m_maximumDate.year());
yearEdit->hide();
spaceHolder = new QSpacerItem(0,0);
QHBoxLayout *headerLayout = new QHBoxLayout;
headerLayout->setMargin(0);
headerLayout->setSpacing(0);
headerLayout->addWidget(prevMonth);
headerLayout->insertStretch(headerLayout->count());
headerLayout->addWidget(monthButton);
headerLayout->addItem(spaceHolder);
headerLayout->addWidget(yearButton);
headerLayout->insertStretch(headerLayout->count());
headerLayout->addWidget(nextMonth);
navBarBackground->setLayout(headerLayout);
yearEdit->setFocusPolicy(Qt::StrongFocus);
prevMonth->setFocusPolicy(Qt::NoFocus);
nextMonth->setFocusPolicy(Qt::NoFocus);
yearButton->setFocusPolicy(Qt::NoFocus);
monthButton->setFocusPolicy(Qt::NoFocus);
//set names for the header controls.
prevMonth->setObjectName(QLatin1String("qt_calendar_prevmonth"));
nextMonth->setObjectName(QLatin1String("qt_calendar_nextmonth"));
monthButton->setObjectName(QLatin1String("qt_calendar_monthbutton"));
yearButton->setObjectName(QLatin1String("qt_calendar_yearbutton"));
yearEdit->setObjectName(QLatin1String("qt_calendar_yearedit"));
updateMonthMenu();
showMonth(m_model->m_date.year(), m_model->m_date.month());
}
通过这个你可以发现QCalendarWidget的构造方式,并且也能找到对应按钮的ObjectName,通过这个ObjectName你就可以把对应的按钮取出来操作,如下:
QToolButton *prevBtn = m_calendarwidget->findChild<QToolButton*>(QLatin1String("qt_calendar_prevmonth"));
QToolButton *nextBtn = m_calendarwidget->findChild<QToolButton*>(QLatin1String("qt_calendar_nextmonth"));
prevBtn->setIcon(QIcon());
nextBtn->setIcon(QIcon());
prevBtn->setText("上一页");
nextBtn->setText("下一页");
这是分别获取上一页、下一页的按钮,然后对这个按钮进行配置。
源码下载地址
版权声明
本文为[东方忘忧]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_43246170/article/details/122210536
边栏推荐
- 汇编 dos中断功能
- Leetcode: Sword finger offer 29 Print the matrix clockwise
- QT add PRI compile run: error: fatal error: no input files problem solving
- AD5724 bipolar ADC
- 动态内存管理、文件操作、预处理
- Part 90 leetcode refers to the longest substring of offer dynamic programming (VII) that does not contain duplicate characters
- Experience of constructing H-bridge with MOS tube
- Part 74 leetcode exercise (VII) 7 Integer inversion
- Jeecgboot Online form Development - control Configuration
- [2022 Ali security] real scene tampering image detection challenge final rank17 scheme sharing
猜你喜欢

stm32单片机与LD3320语音模块交互法二

Photoresist for learning of Blue Bridge Cup embedded expansion board

PyGame simple aircraft war

Part 74 leetcode exercise (VII) 7 Integer inversion

I/O多路复用(select/poll/epoll)

deep learning object detection 精选

Chapter 88 leetcode sword refers to offer dynamic programming (V) maximum value of gifts

Blue Bridge Cup embedded expansion board learning lis302dl

LeetCode: 322. 零钱兑换(动态规划,递归,备忘录递归以及回溯)

wgs84坐标转换,地图拾取wgs84坐标工具推荐
随机推荐
判断完全二叉树
Geojson file ShapeFile file batch conversion gadget
汇编 dos中断功能
opencv图像增强
《C语言程序设计现代方法》阅读笔记
Rtl8367 learning note 2 - network configuration operation literacy
Developing Postgres custom function with C language
The Localtime function affects performance
动态内存管理、文件操作、预处理
Write an article about DDT data-driven automated testing
Assemble DOS interrupt function
STM32 learning note 2 - set GPIO register to realize running water lamp
Part 85 leetcode sword refers to offer dynamic programming (II) frog jumping steps
Part 84 leetcode sword refers to offer dynamic programming (I) Fibonacci sequence
Geojson file and ShapeFile file single conversion gadget
Pykmip test
QT中出现error: undefined reference to `Widget::SetTime()‘
Blue Bridge Cup embedded expansion board learning lis302dl
stm32单片机与LD3320语音模块交互法一
Part 74 leetcode exercise (VII) 7 Integer inversion