一、简介
Qt自带的界面不利于样式的调整和美化,自定义界面便于设计风格。
二、详解
1、代码
(1)pagenumbercontrol.h
#ifndef PAGENUMBERCONTROL_H(2)pagenumbercontrol.cpp
#define PAGENUMBERCONTROL_H
#include <QWidget>
#include "custombtn.h"
#ifndef ABSTRACT
#define ABSTRACT =0
#endif
//usage:IPageNumberControl *app = new PageNumberControl(this);
class IPageNumberControl : public QWidget
{
Q_OBJECT
public:
virtual ~IPageNumberControl(){};
protected:
IPageNumberControl(QWidget *parent = 0):QWidget(parent){};
public:
virtual void setCurrentPage(int currentPageNumber) ABSTRACT;
virtual void setTotalPage(int totalPageNumber) ABSTRACT;
virtual void setCurrentAndTotalPage(int currentPageNumber, int totalPageNumber) ABSTRACT;
virtual void setBackgroundColor(const QColor & color) ABSTRACT;
signals:
void currentPageChanged(int);
};
class PageNumberControl : public IPageNumberControl
{
Q_OBJECT
public:
explicit PageNumberControl(QWidget *parent = 0);
~PageNumberControl();
public:
void setCurrentPage(int currentPageNumber);
void setTotalPage(int totalPageNumber);
void setCurrentAndTotalPage(int currentPageNumber, int totalPageNumber);
void setBackgroundColor(const QColor & color);
protected:
void paintEvent(QPaintEvent *event);
private:
void updatePageNumber();
private slots:
void slotForwardPage();
void slotBackwardPage();
private:
QLabel *currentPageLabel;
QLabel *middleLabel;
QLabel *totalPageLabel;
CustomBtn *forwardBtn;
CustomBtn *backwardBtn;
QPoint dragPosition;
int _currentPageNumber;
int _totalPageNumber;
QColor backgroundColor;
};
#endif // PAGENUMBERCONTROL_H
#include <QtGui>#include "pagenumbercontrol.h"PageNumberControl::PageNumberControl(QWidget *parent) : IPageNumberControl(parent){ // setAutoFillBackground(true); backgroundColor = QColor(0xFF,0xFF,0xFF,0xFF); _currentPageNumber = 1; _totalPageNumber = 1; setStyleSheet("QLabel{font-family:arial;font-size:14px; color:#19649F;}"); currentPageLabel = new QLabel(this); middleLabel = new QLabel(tr("/"), this); totalPageLabel = new QLabel(this); forwardBtn = new CustomBtn(this); forwardBtn->SetImgs(":/images/btn15_bg_hover.png", ":/images/btn15_bg.png", ":/images/btn15_bg_disable.png", ""); forwardBtn->resize(35, 15); forwardBtn->move(0,0); forwardBtn->setText("<", Qt::white, Qt::white, QFont("verdana", 10, QFont::Normal)); connect(forwardBtn, SIGNAL(clicked()), this, SLOT(slotForwardPage())); backwardBtn = new CustomBtn(this); backwardBtn->SetImgs(":/images/btn15_bg_hover.png", ":/images/btn15_bg.png", ":/images/btn15_bg_disable.png", ""); backwardBtn->setText(">", Qt::white, Qt::white, QFont("verdana", 10, QFont::Normal)); backwardBtn->resize(35, 15); backwardBtn->move(18,0); connect(backwardBtn, SIGNAL(clicked()), this, SLOT(slotBackwardPage())); updatePageNumber(); resize(95, 15); setWindowFlags(Qt::FramelessWindowHint);}PageNumberControl::~PageNumberControl(){}void PageNumberControl::setCurrentPage(int currentPageNumber){ if (currentPageNumber > _totalPageNumber) { //超出最大页 _currentPageNumber = _totalPageNumber; } else if (currentPageNumber < 1) { //设置超出最小范围 _currentPageNumber = 1; } else { _currentPageNumber = currentPageNumber; //正常 } updatePageNumber();}void PageNumberControl::setTotalPage(int totalPageNumber) //总页优先级高{ if (totalPageNumber < 1) { //总页设置超出最小范围 if (_currentPageNumber <= 1) _totalPageNumber = 1; else _totalPageNumber = _currentPageNumber; } else if (totalPageNumber < _currentPageNumber){ //总页设置小于当前页 _totalPageNumber = totalPageNumber; _currentPageNumber = totalPageNumber; } else { _totalPageNumber = totalPageNumber; //正常 } updatePageNumber();}void PageNumberControl::setCurrentAndTotalPage(int currentPageNumber, int totalPageNumber){ if (currentPageNumber > totalPageNumber) //当前页范围过大,以总页数为优先级 _currentPageNumber = totalPageNumber; setTotalPage(totalPageNumber); setCurrentPage(currentPageNumber);}void PageNumberControl::slotForwardPage() //向前翻页{ _currentPageNumber--; emit currentPageChanged(_currentPageNumber); //发送页面改变信号 updatePageNumber();}void PageNumberControl::slotBackwardPage() //向后翻页{ _currentPageNumber++; emit currentPageChanged(_currentPageNumber); //发送页面改变信号 updatePageNumber();}void PageNumberControl::updatePageNumber(){ //翻页更新 if (_currentPageNumber <= 1) { forwardBtn->enabled(false); } else { forwardBtn->enabled(true); } if (_currentPageNumber >= _totalPageNumber) { backwardBtn->enabled(false); } else { backwardBtn->enabled(true); } QFontMetrics currentMetrics(currentPageLabel->font()); int currentTextWidth = currentMetrics.width(QString::number(_currentPageNumber)); int currentTextHeight = currentMetrics.height(); QFontMetrics totalMetrics(totalPageLabel->font()); int totalTextWidth = totalMetrics.width(QString::number(_totalPageNumber)); int totalTextHeight = totalMetrics.height(); currentPageLabel->resize(currentTextWidth, currentTextHeight); totalPageLabel->resize(totalTextWidth + 1, totalTextHeight); currentPageLabel->move(38, 0); middleLabel->move(39 + currentTextWidth, 0); totalPageLabel->move(44 + currentTextWidth, 0); currentPageLabel->setText(QString::number(_currentPageNumber)); totalPageLabel->setText(QString::number(_totalPageNumber));}void PageNumberControl::setBackgroundColor(const QColor &color){ backgroundColor = color; update();}void PageNumberControl::paintEvent(QPaintEvent *event){ QPalette pal = palette(); pal.setColor(QPalette::Background, backgroundColor); setPalette(pal); QWidget::paintEvent(event);}(3)selecttemplate.h
#ifndef SELECTTEMPLATE_H#define SELECTTEMPLATE_H#include <QtGui>#include "custombtn.h"#include "pagenumbercontrol.h"class SelectTemplate : public QDialog{ Q_OBJECTpublic: explicit SelectTemplate(QWidget *parent = 0);protected: void mousePressEvent(QMouseEvent *event); void mouseMoveEvent(QMouseEvent *event); void mouseReleaseEvent(QMouseEvent *event); void paintEvent(QPaintEvent *event);private: bool bPressFlag; QPoint dragPosition; QPixmap backGroundPix; CustomBtn *closeBtn; CustomBtn *confirmBtn; QButtonGroup *group; PageNumberControl *turnPage;signals:public slots: void slotButtonGroup(QAbstractButton * button); void slotButtonGroup(int id); void slotConfirmBtn();};#endif // SELECTTEMPLATE_H(4)selecttemplate.cpp
#include "selecttemplate.h"SelectTemplate::SelectTemplate(QWidget *parent) : QDialog(parent), bPressFlag(false){ /***************setup********************/ setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint); setAutoFillBackground(false); QPalette pal = palette(); pal.setColor(QPalette::Background, QColor(0xFF,0xFF,0xFF,0xFF)); setPalette(pal); backGroundPix = QPixmap(":/images/client_bg.png"); resize(backGroundPix.width(), backGroundPix.height()); confirmBtn = new CustomBtn(this); confirmBtn->SetImgs(":/images/btn120_bg_hover.png", ":/images/btn120_bg.png" , "", ":/images/btn120_bg_hover.png"); confirmBtn->setText(QString(tr("<<确认选择"))); confirmBtn->move(185, 325); connect(confirmBtn, SIGNAL(clicked()), this, SLOT(slotConfirmBtn())); QListWidget *applistWidget = new QListWidget(this); applistWidget->move(150, 60); applistWidget->resize(280,250); applistWidget->setStyleSheet("background-color:#ffffff;border-top: 1px solid #949494;" "border-right: 1px solid #949494;border-bottom:1px solid #949494;"); applistWidget->setFocusPolicy(Qt::NoFocus); //applistWidget->setEnabled(false); QListWidgetItem *softWareOne = new QListWidgetItem; softWareOne->setIcon(QIcon(":/images/appList_icon.png")); softWareOne->setText(tr("ADobe Dreamweaver CS6")); softWareOne->setSizeHint(QSize(35,32)); applistWidget->addItem(softWareOne); QListWidgetItem *softWareTwo = new QListWidgetItem; softWareTwo->setIcon(QIcon(":/images/appList_icon.png")); softWareTwo->setText(tr("Flash Player")); softWareTwo->setSizeHint(QSize(35,32)); applistWidget->addItem(softWareTwo); group = new QButtonGroup(this); for(int i = 0;i < 7; i++) { QRadioButton *radioBtn = new QRadioButton(this); radioBtn->setFocusPolicy(Qt::NoFocus); radioBtn->setText(QString(tr("类型%1").arg(i+1))); radioBtn->setStyleSheet("background-color:#c2e7f9;padding-left:4px;color:#19649f;"); radioBtn->move(40, i*30 + 70); group->addButton(radioBtn, i); } connect(group, SIGNAL(buttonClicked(QAbstractButton *)), this, SLOT(slotButtonGroup(QAbstractButton *))); connect(group, SIGNAL(buttonClicked(int)), this, SLOT(slotButtonGroup(int))); for (int i = 0;i <7; i++){ QLabel *label = new QLabel(this); label->resize(105,3); label->setFrameStyle(QFrame::HLine | QFrame::Raised); label->setLineWidth(1); label->move(40, i*30 + 90); } turnPage = new PageNumberControl(this); //control pagenumber turnPage->setCurrentAndTotalPage(2, 10); turnPage->move(40, 285); //connect(turnPage, SIGNAL(currentPageChanged(int)),this, SLOT(slotTurnPage(int))); closeBtn = new CustomBtn(this); closeBtn->SetImgs(":/images/dialog_btn_close_hover.png",":/images/dialog_btn_close.png","",":/images/dialog_btn_close_hover.png"); closeBtn->setGeometry(width() - 38, 5, 32, 32); closeBtn->show(); connect(closeBtn, SIGNAL(clicked()), this, SLOT(close())); move((QApplication::desktop()->width() - width())/2, (QApplication::desktop()->height() - height())/2);}void SelectTemplate::paintEvent(QPaintEvent *event){ QPainter painter(this); painter.drawPixmap(0, 0, backGroundPix.width(), backGroundPix.height(), backGroundPix); painter.setPen(QPen(QColor("#2676A1"), 2)); painter.setFont(QFont("", 14, QFont::Black)); painter.drawText(QRect(190, 35, 400, 40), QString(tr("选择模板"))); painter.setPen(Qt::NoPen); painter.setBrush(QColor("#c2e7f9")); painter.drawRect(35,60,115,250); painter.setPen(QPen(QColor("#d4d4d4"), 1)); painter.drawLine(0, height() - 1, width(), height() -1); QDialog::paintEvent(event);}void SelectTemplate::slotButtonGroup(QAbstractButton * button){ qDebug("-----------QAbstractButton---------------"); qDebug() << button->text(); qDebug() << group->checkedId(); qDebug() << group->checkedButton(); if (group->checkedButton()) { qDebug() << group->checkedButton()->text(); }}void SelectTemplate::slotButtonGroup(int id){ qDebug("-----------int---------------"); qDebug() << id; qDebug() << group->button(id)->text(); qDebug() << group->checkedId(); qDebug() << group->checkedButton(); if (group->checkedButton()) { qDebug() << group->checkedButton()->text(); }}void SelectTemplate::slotConfirmBtn(){ qDebug() << "---------------final----------------"; qDebug() << group->checkedId(); qDebug() << group->checkedButton(); qDebug() << group->checkedButton()->text(); close();}/****************move everywhere*******************/void SelectTemplate::mousePressEvent ( QMouseEvent * event){ bPressFlag = true; dragPosition = event->pos(); QDialog::mousePressEvent(event);}void SelectTemplate::mouseMoveEvent(QMouseEvent *event){ if (bPressFlag) { QPoint relaPos(QCursor::pos() - dragPosition); move(relaPos); } QDialog::mouseMoveEvent(event);}void SelectTemplate::mouseReleaseEvent(QMouseEvent *event){ bPressFlag = false; QDialog::mouseReleaseEvent(event);}(5)applicationinfo.h
#ifndef DIALOG_H#define DIALOG_H#include <QDialog>#include "custombtn.h"#include "vmcontrol.h"#include <QtGui>class ApplicationInfo : public QWidget{ Q_OBJECTpublic: ApplicationInfo(QWidget *parent = 0); ~ApplicationInfo();private: CustomBtn *_closeBtn; VmmComboBox *opSystemType; VmmComboBox *confTemplate; VmmComboBox *dataDisk; VmmComboBox *chooseTemplate; QLabel *titleLabel; QLabel *opSystemTypeLabel; QLabel *confTemplateLabel; QLabel *dataDiskLabel; QLabel *chooseTemplateLabel; QLabel *btnLabel; CustomBtn *submitApplyBtn; CustomBtn *templateBtn; QMenu *_menu; QMenu *_disk;protected: void mousePressEvent(QMouseEvent * event); void mouseMoveEvent(QMouseEvent * event); void mouseReleaseEvent(QMouseEvent *event); void paintEvent(QPaintEvent * event);private: QPoint dragPosition; bool bPressFlag;private slots: void slotSubmit(); void slotChooseTemplate(); void slotSysNav(); void applyDisk(QAction *action);};#endif // DIALOG_H(6)applicationinfo.cpp
#include "applicationinfo.h"#include "selecttemplate.h"ApplicationInfo::ApplicationInfo(QWidget *parent) : QWidget(parent) ,bPressFlag(false){ opSystemType = new VmmComboBox(this, QString(tr("operating system"))); opSystemType->resizeWidth(232); opSystemType->move(180,75); opSystemType->insertItem(0, tr("General")); opSystemType->insertItem(1, tr("Linux older)")); opSystemType->insertItem(2, tr("Linux newer")); opSystemType->insertItem(3, "Windows XP"); opSystemType->setCurrentIndex(3); confTemplate = new VmmComboBox(this, QString(tr("configure template"))); confTemplate->resizeWidth(232); confTemplate->move(180, 125); confTemplate->insertItem(0, tr("1Cpus 10GBMemory")); confTemplate->insertItem(1, tr("2Cpus 20GBMemory")); confTemplate->insertItem(2, tr("3Cpus 30GBMemory")); confTemplate->setCurrentIndex(0); dataDisk = new VmmComboBox(this, QString(tr("Select a data disk"))); dataDisk->resizeWidth(232); dataDisk->move(180, 175); dataDisk->insertItem(0, tr("10GB")); dataDisk->insertItem(0, tr("20GB")); dataDisk->setCurrentIndex(0); chooseTemplate = new VmmComboBox(this, QString(tr("Select a template"))); chooseTemplate->resizeWidth(232); chooseTemplate->move(180, 225); chooseTemplate->insertItem(0, tr("Default")); chooseTemplate->setCurrentIndex(0); btnLabel = new QLabel(this); btnLabel->setStyleSheet("background:rgba(255,255,255,255);"); templateBtn = new CustomBtn(this); templateBtn->SetImgs(":/images/temple_btn_hover.png", ":/images/temple_btn.png","", ":/images/temple_btn_hover.png"); connect(templateBtn, SIGNAL(clicked()), this, SLOT(slotChooseTemplate())); btnLabel->move(378,227); btnLabel->resize(22,30); templateBtn->move(378,226); templateBtn->resize(32,33); submitApplyBtn = new CustomBtn(this); submitApplyBtn->SetImgs(":/images/dialogBtn_hover.png", ":/images/dialogBtn.png" , "", ":/images/dialogBtn.png"); submitApplyBtn->move(185, 275); submitApplyBtn->setText(QString(tr("提交"))); connect(submitApplyBtn, SIGNAL(clicked()), this, SLOT(slotSubmit())); setAutoFillBackground(true); setWindowFlags(Qt::FramelessWindowHint); // | Qt::WindowStaysOnTopHint); QPalette p; p.setBrush(QPalette::Window, QBrush(QColor("#EDF5F9"))); //color this->setPalette(p); //setWindowState(Qt::WindowFullScreen); resize(450, 350); CustomBtn *_sysNav = new CustomBtn(this); _sysNav->SetHoverImg(":/images/window_sysNav_hover.png"); _sysNav->SetNormalImg(":/images/window_sysNav.png"); _sysNav->move(0, 0); connect(_sysNav, SIGNAL(clicked()), this, SLOT(slotSysNav())); setStyleSheet("QMenu::item {background-color: #ebf6fd;padding: 3px 10px 3px 20px;border: 0px solid transparent;color: #19649f" "}QMenu::item:selected {background-color: #006699;color: white} QMenu::item:!enabled{color: gray}"); _menu = new QMenu(this); _disk = new QMenu(tr("Apply Disk "), _menu); _menu->addMenu(_disk); for (int i = 1; i < 10; i++) { _disk->addAction(QString("%1GB").arg(10 * i)); } connect(_disk , SIGNAL(triggered(QAction*)), this, SLOT(applyDisk(QAction*))); QAction *account = _menu->addAction(tr("Account")); QAction *about = _menu->addAction(tr("about")); QAction *exit = _menu->addAction(tr("exit")); connect(exit, SIGNAL(triggered()), this, SLOT(close())); _closeBtn = new CustomBtn(this); _closeBtn->SetImgs(":/images/dialog_btn_close_hover.png",":/images/dialog_btn_close.png","",":/images/dialog_btn_close_hover.png"); _closeBtn->setGeometry(width() - 32, 0, 32, 32); _closeBtn->show(); connect(_closeBtn, SIGNAL(clicked()), this, SLOT(close())); move((QApplication::desktop()->width() - width())/2, (QApplication::desktop()->height() - height())/2);}ApplicationInfo::~ApplicationInfo(){}void ApplicationInfo::paintEvent(QPaintEvent *event){ QPainter painter(this); painter.setPen(QPen(QColor("#333333"))); painter.setFont(QFont("", 11, QFont::Normal)); painter.drawText(QRect(50, opSystemType->pos().y() + 10, 200, 20), QString(tr("system:"))); painter.drawText(QRect(50, confTemplate->pos().y() + 10, 200, 20), QString(tr("template :"))); painter.drawText(QRect(50, dataDisk->pos().y() + 10, 200, 20), QString(tr("datadisk:"))); painter.drawText(QRect(50, chooseTemplate->pos().y() + 10, 200, 20), QString(tr("template:"))); painter.setPen(QPen(QColor("#2676A1"), 2)); painter.setFont(QFont("", 16, QFont::Black)); painter.drawText(QRect(200, 25, 400, 40), QString(tr("信息显示"))); QWidget::paintEvent(event);}void ApplicationInfo::slotSubmit(){ QMessageBox::information(this,tr("info"),tr("ok!"));}void ApplicationInfo::slotChooseTemplate(){ SelectTemplate *templateInfo = new SelectTemplate; templateInfo->move(this->pos()); templateInfo->setModal(true); templateInfo->show();}void ApplicationInfo::slotSysNav(){ //_menu->popup(this->mapToGlobal( QPoint(_sysNav->geometry().left() - 140, 20))); _menu->popup(mapToGlobal(QPoint(15, 0)));}void ApplicationInfo::applyDisk(QAction * action){ qDebug() << action->text();}void ApplicationInfo::mousePressEvent ( QMouseEvent * event){ bPressFlag = true; dragPosition = event->pos(); QWidget::mousePressEvent(event);}void ApplicationInfo::mouseMoveEvent(QMouseEvent *event){ if (bPressFlag) { QPoint relaPos(QCursor::pos() - dragPosition); move(relaPos); } QWidget::mouseMoveEvent(event);}void ApplicationInfo::mouseReleaseEvent(QMouseEvent *event){ bPressFlag = false; QWidget::mouseReleaseEvent(event);}
(7)waitingwidget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QtGui>
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = 0);
~Widget();
void start();
void stop();
void setMaskWidget(bool flag);
private slots:
void slotRotatePic(qreal value);
private:
QTimeLine *timeline;
int picture_number;
int counter;
QWidget *maskWidget;
QLabel *waitingLabel;
};
#endif // WIDGET_H
(8)waitingwidget.cpp
#include "waitingwidget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, picture_number(12)
, counter(0)
{
setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
setAutoFillBackground(true);
QPalette pal = palette();
pal.setColor(QPalette::Background, QColor(0xFF,0xFF,0xFF,0x00));
setPalette(pal);
setVisible(false);
//resize(42, 42);
timeline=new QTimeLine(picture_number*1000,this);
timeline->setFrameRange(1,picture_number);
timeline->setUpdateInterval(50);
timeline->setLoopCount(0);
waitingLabel = new QLabel(this);
waitingLabel->resize(42, 42);
maskWidget = new QWidget(this, Qt::CustomizeWindowHint | Qt::FramelessWindowHint);
maskWidget->setStyleSheet( "background-color:rgba(0, 0, 0,35)");
maskWidget->hide();
if (parent) {
maskWidget->resize(parent->size());
waitingLabel->move((parent->width() - waitingLabel->width())/2, (parent->height() - waitingLabel->height())/2);
this->move(parent->pos());
}
connect(timeline,SIGNAL(valueChanged(qreal)),this,SLOT(slotRotatePic(qreal)));
}
Widget::~Widget()
{
}
void Widget::start()
{
timeline->start();
timeline->setLoopCount(0);
this->setVisible(true);
}
void Widget::stop()
{
timeline->stop();
this->setVisible(false);
}
void Widget::slotRotatePic(qreal value)
{
if(counter > 11) counter = 0;
QPixmap backGroundPix(QString(":/images/loading_%1.png").arg(counter+1));
backGroundPix = backGroundPix.scaled(QSize(42, 42), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
waitingLabel->setPixmap(backGroundPix);
update();
counter++;
}
void Widget::setMaskWidget(bool flag)
{
if (flag == true) maskWidget->show();
else maskWidget->hide();
}
2、运行
三、总结
(1)上述代码仅提供思路参考,只有界面的显示,也还有一些功能bug。(2)若需要相应的界面效果可以发邮件联系,aoyang888@qq.com。
(3)若有问题或建议,请留言,在此感谢!