赞
踩
- #include <QDesktopWidget>
- #include <QApplication>
-
- setWindowFlags(windowFlags() & ~Qt::WindowMinMaxButtonsHint);
- setMinimumSize(LANCHERWIDGET_WIDTH ,LANCHERWIDGET_HEIGHT);
- setMaximumSize(LANCHERWIDGET_WIDTH ,LANCHERWIDGET_HEIGHT);
- setAutoFillBackground(true);
- setAttribute(Qt::WA_TranslucentBackground);
-
- QDesktopWidget *deskdop = QApplication::desktop();
- int startX = (deskdop->width() - LANCHERWIDGET_WIDTH)/2;
- int startY = (deskdop->height() - LANCHERWIDGET_HEIGHT)/2;
- setGeometry(startX, startY,LANCHERWIDGET_WIDTH,LANCHERWIDGET_HEIGHT);
- void XX::paintEvent(QPaintEvent* event)
- {
- Q_UNUSED(event);
- QPainter p(this);
- p.save();
- p.setRenderHint(QPainter::Antialiasing, true);
- p.drawPixmap(rect(),QPixmap(":/images/background.png"));
- p.restore();
-
- }
- setAutoFillBackground(true);
- QPalette palette;
- palette.setColor(QPalette::Background,Qt::green);
- setPalette(palette);
- private:
- QPoint last_mouse_position;
- protected:
- void mousePressEvent(QMouseEvent* event);
- void mouseMoveEvent(QMouseEvent* event);
- void mouseReleaseEvent(QMouseEvent* event);
- void XX::mousePressEvent(QMouseEvent* event)
- {
- if (event->button() == Qt::LeftButton)
- {
- last_mouse_position = event->globalPos();
- }
- }
-
- void XX::mouseMoveEvent(QMouseEvent* event)
- {
- if (event->buttons() & Qt::LeftButton)
- {
- const QPoint position = event->globalPos() - last_mouse_position;
- //qDebug()<<"POSY:"<<last_mouse_position<<""<<event->globalPos()<<" "<< position;
- }
- }
-
- void XX::mouseReleaseEvent(QMouseEvent* event)
- {
- Q_UNUSED(event);
- //emit moveDoneSignal();
- }
setAttribute(Qt::WA_StyledBackground,true);
- void XX::paintEvent(QPaintEvent *event)
- {
- Q_UNUSED(event);
-
- QStyleOption opt;
- opt.init(this);
- QPainter pt(this);
- pt.setRenderHint(QPainter::Antialiasing, true);
- style()->drawPrimitive(QStyle::PE_Widget, &opt, &pt, this);
- QWidget::paintEvent(event);
- // QPainter p(this);
- // p.save();
- // p.setRenderHint(QPainter::Antialiasing, true);
- // p.drawPixmap(rect(),QPixmap(":/img/loginbkg.png"));
- // p.restore();
- }
《Qt控件的使用》
- //设置样式表
- QStringList list;
- list << "#titleBar{background:#BBBBBB;}";
- list << "#titleBar{border-top-left-radius:8px;border-top-right-radius:8px;}";
- list << "#widgetMain{border:2px solid #BBBBBB;background:#FFFFFF;}";
- //list << "#widgetMain{border-bottom-left-radius:8px;border-bottom-right-radius:8px;}";
- this->setStyleSheet(list.join(""));
- this->setObjectName("loginPage");
- this->setStyleSheet("QWidget#loginPage{border-image:url(:/core/images/app/login_background.png);}");
- QComboBox {
- border: 1px solid #aaaaaa;
- border-radius: 2px;
- padding: 1px 18px 1px 3px;
- font-family: "Source Han Sans CN Regular";
- font-size: 13px;
- height: 28px;
- }
-
- QComboBox:editable {
- background: white;
- }
-
-
- QComboBox QAbstractItemView::item {
- height: 28px; /* 项的高度(设置pComboBox->setView(new QListView(this));后,该项才起作用) */
- }
-
- /* 设置为可编辑editable时,点击下拉框的样式 */
- QComboBox::drop-down:editable:on {
- background: #dadada;
- }
-
- /* 设置为非编辑!editable时,下拉框的样式 */
- QComboBox::drop-down:!editable {
- background: #dadada;
- }
-
- /* 设置为非编辑!editable时,点击下拉框的样式 */
- QComboBox::drop-down:!editable:on {
- background: #dadada;
- }
-
- /* 点击QComboBox */
- QComboBox:on {
- }
-
-
- /* 下拉框样式 */
- QComboBox::drop-down {
- subcontrol-origin: padding; /* 子控件在父元素中的原点矩形。如果未指定此属性,则默认为padding。 */
- subcontrol-position: top right; /* 下拉框的位置(右上) */
- width: 15px; /* 下拉框的宽度 */
-
- border-left-width: 1px; /* 下拉框的左边界线宽度 */
- border-left-color: darkgray; /* 下拉框的左边界线颜色 */
- border-left-style: solid; /* 下拉框的左边界线为实线 */
- border-top-right-radius: 3px; /* 下拉框的右上边界线的圆角半径(应和整个QComboBox右上边界线的圆角半径一致) */
- border-bottom-right-radius: 3px; /* 同上 */
- }
-
- /* 越过下拉框样式 */
-
- QComboBox::drop-down:hover {
-
- background: yellow;
-
- }
-
- QComboBox::down-arrow {
- border-image: url(:/assets/imges/down_arrow.png);
- width: 28px;
- background: transparent;
- padding: 5px 0px 5px 0px;
-
- }
-
-
- QComboBox::down-arrow:on {
- border-image: url(:/assets/imges/down_up.png); /* 显示上拉箭头 */
- }
- QPushButton[class=RouterBtnType1]{
- background: #FFFFFF;
- border-radius: 2px;
- /* border: 1px solid rgba(0,0,0,0.15);*/
- border: 1px solid #aaaaaa;
- font-family: "Source Han Sans CN Medium";
- font-size: 13px;
- height: 28px;
- color: #333333;
- }
-
- QPushButton[class=RouterBtnType1]:hover{
- border: 1px solid #4CACFD;
- color: #4CACFD;
- }
-
- QPushButton[class=RouterBtnType1]:pressed{
- background: #4CACFD;
- color: #FFFFFF;
- }
-
- QPushButton[class=RouterBtnType2]{
- background: #3f9dfd;
- border-radius: 4px;
- /* border: 1px solid rgba(0,0,0,0.15);*/
- border: none;
- height: 30px;
- font-size: 13px;
- font-family: "Source Han Sans CN Medium";
- color: #FFFFFF;
- }
-
-
- QPushButton[class=RouterBtnType2]:hover{
- background: #67b2ff;
- }
-
- QPushButton[class=RouterBtnType2]:pressed{
- background: #3a8de4;
- }
- QPushButton[class=RouterBtnType2]:disabled{
- background: #dadada;
- color: #aaaaaa;
- }
- QLineEdit{
- border: 1px solid #aaaaaa;
- border-radius: 2px;
- font-family: "Source Han Sans CN Regular";
- font-size: 13px;
- /* width: 200px;*/
- height: 28px;
- }
- #ifndef COMMONHELPER_H
- #define COMMONHELPER_H
-
- #include <QFile>
- //#include <QApplication>
- #include <QWidget>
- #include <QPixmap>
- #include <QLabel>
- #include <QStyle>
-
-
- class CommonHelper
- {
- public:
- CommonHelper();
-
- public:
- static void setStyle(QWidget* wnd,const QString &style) {
- if(!wnd) return;
- QFile qss(style);
- qss.open(QFile::ReadOnly);
- wnd->setStyleSheet(qss.readAll());
- qss.close();
- }
-
- static void setLabelIcon(QLabel* lab, QString url, int size){
- if(!lab) return;
- QPixmap pixmap = QPixmap::fromImage(QImage(url));
- QPixmap fitpixmap = pixmap.scaled(size, size, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
- lab->setScaledContents(true);
- lab->setPixmap(fitpixmap);
- }
-
- static void updateWidget(QWidget* w){
- if(!w) return;
- w->style()->unpolish(w);
- w->style()->polish(w);
- w->update();
- }
-
-
- };
-
- #endif // COMMONHELPER_H
- //滑动条样式表
- QString scrollBarStyle = QString(
- "QScrollBar{background:transparent; width:10px; margin:0px 0px 0px 0px;}"
- "QScrollBar::handle{background:rgba(63, 64, 65, 200); border:0px; border-radius:5px; margin:0px 0px 0px 0px;}"
- "QScrollBar::handle:hover{background:lightgray;}"
- "QScrollBar::handle:pressed{background:rgba(200, 200, 200, 255);}"
- "QScrollBar::sub-page{background:transparent;}"
- "QScrollBar::add-page{background:transparent;}"
- "QScrollBar::up-arrow{background:transparent;}"
- "QScrollBar::down-arrow{background:transparent;}"
- "QScrollBar::sub-line{background:transparent;}"
- "QScrollBar::add-line{background:transparent;}"
- );
-
- //滑动条
- QScrollBar *scrollBar = new QScrollBar(m_listView);
- scrollBar->setStyleSheet(scrollBarStyle);
- m_listView->setVerticalScrollBar(scrollBar);
- m_listView->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
- QScrollBar{background:transparent; width:10px; margin:0px 0px 0px 0px;}
- QScrollBar::handle{background:rgba(63, 64, 65, 200); border:0px; border-radius:5px; margin:0px 0px 0px 0px;}
- QScrollBar::handle:hover{background:lightgray;}
- QScrollBar::handle:pressed{background:rgba(200, 200, 200, 255);}
- QScrollBar::sub-page{background:transparent;}
- QScrollBar::add-page{background:transparent;}
- QScrollBar::up-arrow{background:transparent;}
- QScrollBar::down-arrow{background:transparent;}
- QScrollBar::sub-line{background:transparent;}
- QScrollBar::add-line{background:transparent;}
- //主布局
- QVBoxLayout* vMainBox = new QVBoxLayout();
- vMainBox->addWidget(topContainer);
- vMainBox->addWidget(signalStatusWidget);
- vMainBox->addWidget(routerConfWidget);
- vMainBox->setMargin(0);
- vMainBox->setSpacing(SHADOW_WIDTH);
- vMainBox->setContentsMargins(SHADOW_WIDTH, SHADOW_WIDTH, SHADOW_WIDTH, SHADOW_WIDTH);
-
- QWidget * mainContainer = new QWidget(this);
- mainContainer->setObjectName("mainContainer");
- mainContainer->setLayout(vMainBox);
-
- QScrollArea *scrollArea = new QScrollArea(this);
- scrollArea->setWidgetResizable(true);
- scrollArea->setWidget(mainContainer);
-
- QHBoxLayout *hMainLayout = new QHBoxLayout;
- hMainLayout->addWidget(scrollArea);
- hMainLayout->setMargin(0);
- hMainLayout->setSpacing(0);
- hMainLayout->setContentsMargins(0, 0, 0, 0);
- this->setLayout(hMainLayout);
- QScrollArea *scrollArea = new QScrollArea(this);
- scrollArea->setObjectName("FancyTabScrollArea");
- scrollArea->setWidget(m_modesStackContainer);
- scrollArea->setWidgetResizable(true);
-
- m_vSplitter = new QSplitter(Qt::Vertical, this);
- m_vSplitter->addWidget(scrollArea);
- m_vSplitter->addWidget(m_statusWidget);
- m_vSplitter->setChildrenCollapsible(false);
- m_statusWidget->setMinimumHeight(CCSTATUSWIDGET_MIN_HEIGHT);
- m_vSplitter->setSizes(QList<int>{9999, 1});
- m_splitterList = m_vSplitter->sizes();
- int loadedFontID = QFontDatabase::addApplicationFont(":/assets/fonts/SourceHanSansCN-Medium.ttf");
- QStringList loadedFontFamilies = QFontDatabase::applicationFontFamilies(loadedFontID);
- if (!loadedFontFamilies.empty())
- {
- QString sansCNFamily = loadedFontFamilies.at(0);
- qDebug() << " sansCNFamily " << sansCNFamily;
- QFont defaultFont = app.font();
- defaultFont.setFamily(sansCNFamily);
-
- //设置字体大小格式为pixel,字体占用固定像素。相同像素的大小屏,不会发生字体遮挡现象
- defaultFont.setPixelSize(13);
- app.setFont(defaultFont);
- }
- void loadFonts()
- {
- const QString creatorPath = QCoreApplication::applicationDirPath();
- QString dir = creatorPath + QLatin1String("/../share/fonts");
-
- const QFileInfoList fonts = QDir(dir).entryInfoList(QStringList("*.ttf"), QDir::Files);
- for (const QFileInfo &fileInfo : fonts)
- {
- QFontDatabase::addApplicationFont(fileInfo.absoluteFilePath());
- qDebug() << "fileInfo.absoluteFilePath() " << fileInfo.absoluteFilePath();
- }
- }
- QFont defaultFont = app.font();
- defaultFont.setFamily("Source Han Sans CN Regular");
- defaultFont.setPixelSize(13);
- app.setFont(defaultFont);
-
- QPalette defaultPalette = app.palette();
- defaultPalette.setColor(QPalette::WindowText, QColor(51,51,51));
- app.setPalette(defaultPalette);
- #include <QGraphicsDropShadowEffect>
-
- #define SHADOW_WIDTH 15
-
- void XXWidget::addShadow(QWidget *w)
- {
- //设置具体阴影
- QGraphicsDropShadowEffect* shadowEffect = new QGraphicsDropShadowEffect(w);
- // 阴影偏移
- shadowEffect->setOffset(0, 0);
- // 阴影颜色;
- shadowEffect->setColor(QColor(200, 200, 200, 220));
- // 阴影半径;
- shadowEffect->setBlurRadius(SHADOW_WIDTH);
- // 给窗口设置上当前的阴影效果;
- w->setGraphicsEffect(shadowEffect);
-
- }
-
-
- topContainer 布局 vMainBox->setContentsMargins(15, 10, 15, 10);
- topContainer 颜色 #FFFFFF
-
- addShadow(topContainer);
- QCheckBox {
- spacing: 5px;
- }
-
- QCheckBox::indicator {
- width: 18px;
- height: 18px;
- }
-
- QCheckBox::indicator:unchecked {
- image: url(:/assets/imges/checkbox_unchecked.png);
- }
-
-
- QCheckBox::indicator:checked {
- image: url(:/assets/imges/checkbox_checked.png);
- }
- setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
-
- 固定窗口大小
- setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint | Qt::MSWindowsFixedSizeDialogHint);
- #include <QGridLayout>
-
- QWidget* topWidget = new QWidget(this);
- QWidget* bottomWidget = new QWidget(this)
- QGridLayout* gridBox = new QGridLayout(t
- gridBox->addWidget(topWidget, 0, 0, 1, 5)
- gridBox->addWidget(bottomWidget, 5, 0, 1,
- gridBox->setMargin(0);
- int margin = 30;
- gridBox->setContentsMargins(margin, margi
- gridBox->setSpacing(20);
- this->setLayout(gridBox);
- double scale_;
- QPointF startPos_;
- qreal scaleValue_;
- bool isMove_;
-
-
- void MyCanvasView::wheelEvent(QWheelEvent *event)
- {
- if (event->modifiers() == Qt::CTRL)
- {
- //按住ctrl键 可以放大缩小
- if((event->delta() > 0)&&(scale_ >= 50))//最大放大到原始的50倍
- {
- return;
- }
- else if((event->delta() < 0)&&(scale_ <= 0.01))//缩小到自适应大小之后就不继续缩小
- {
- return;//重置大小和位置,使之自适应控件窗口大小
- }
- else
- {
- // 当前放缩倍数;
- qreal scaleFactor = this->matrix().m11();
- scale_ = scaleFactor;
-
- int wheelDeltaValue = event->delta();
- // 向上滚动,放大;
- if (wheelDeltaValue > 0)
- {
- this->scale(1.2, 1.2);
- }
- else
- {
- // 向下滚动,缩小;
- this->scale(1.0 / 1.2, 1.0 / 1.2);
- }
- update();
- }
-
- return;
- }
-
- QGraphicsView::wheelEvent(event);
-
- }
-
- void MyCanvasView::mousePressEvent(QMouseEvent *event)
- {
- if(event->button()== Qt::LeftButton && event->modifiers() == Qt::CTRL)
- {
- startPos_ = event->pos();//鼠标左击时,获取当前鼠标在图片中的坐标,
- isMove_ = true; //标记鼠标左键被按下
- viewport()->setCursor(Qt::PointingHandCursor);
- return;
- }
- QGraphicsView::mousePressEvent(event);
- }
-
- void MyCanvasView::mouseMoveEvent(QMouseEvent *event)
- {
- if(isMove_ && event->modifiers() == Qt::CTRL )
- {
-
- auto hBar = horizontalScrollBar();
- auto vBar = verticalScrollBar();
- auto delta = event->pos() - startPos_;
- startPos_ = event->pos();
- hBar->setValue(hBar->value() - delta.x());
- vBar->setValue(vBar->value() - delta.y());
-
- return;
- }
- QGraphicsView::mouseMoveEvent(event);
- }
-
- void MyCanvasView::mouseReleaseEvent(QMouseEvent *event)
- {
- viewport()->setCursor(Qt::ArrowCursor);
- isMove_ = false;//标记鼠标左键已经抬起
- QGraphicsView::mouseReleaseEvent(event);
- }
IDI_ICON1 ICON DISCARDABLE "app.ico"
- if (WIN32)
- list(APPEND DIR_LIB_SRCS app.rc)
- endif()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。