使用java和qt开发远程控制系统-主界面设计

首界面效果如图使用java和qt开发远程控制系统-主界面设计_第1张图片

界面就比较简单,拖拉几个控件,设置一下样式ok,不得不说QT这个QSS弄的还是可以,虽然明显是抄袭CSS,语法基本一样,但就是香,比微软那MFC好用太多了。

整个界面还增加了一个标题栏,以及窗体圆角边框和阴影效果,具体是实现方式为,定义了一个MDialog类,继承自QDialog,在MDialog里面,实现了圆角边框,阴影效果,使用绘图方式实现的。

新建一个Dialog,只要继承一下MDialog,就可以轻松实现。需要注意的是,ui文件里面也需要修改一下继承类,但是在QTCreater里面无法修改,因此需要用记事本打开ui文件,手动修改继承自MDiaolog类使用java和qt开发远程控制系统-主界面设计_第2张图片

 图的意思就是ui文件需要修改基类

MDialog.h源码

#include 
#include "qlabel.h"
#include "qpushbutton.h"
#include "QPoint"
#include 

class MDialog : public QDialog
{
    Q_OBJECT

public:
    explicit MDialog(QWidget *parent = 0);
    ~MDialog();
    void show();
protected:
    virtual void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE;
    //virtual void keyPressEvent(QKeyEvent *ev);
    void mouseMoveEvent(QMouseEvent* event);//鼠标移动事件
    void mousePressEvent(QMouseEvent* event);//鼠标按下事件
    void mouseReleaseEvent(QMouseEvent* event);//鼠标释放事件
    QPushButton *closeBtn;
private:

    QWidget * top;
    QLabel * title;

    QLabel *ico;//用来显示图标

    bool isPressed;
    QPoint old;//原始的点
    int x_nBorder;//边框
    QColor x_bgColor;//背景色
    int x_nRatio;//圆角率
    void drawBG(QPainter* painter);

    void drawShadow(QPainter* painter);
public slots:
    void doClose();
    void doMin();
    void doMenu();
public:
    void hideTitle();
    void hideMenu();
    void setTitle(QString title);//设置标题
    void hideMin();//隐藏最小化
    void hideClose();//隐藏关闭按钮
    void hideIcon();//隐藏图标

};

#endif // MFORM_H

MDialog.cpp源码

#include "mdialog.h"

#include "qpainter.h"
#include "QDebug"
#include 
#include 
MDialog::MDialog(QWidget *parent) :
    QDialog(parent)
{
    //ui->setupUi(this);
    this->setAttribute(Qt::WA_TranslucentBackground);
    setWindowFlags(Qt::FramelessWindowHint| Qt::WindowStaysOnTopHint);

    x_nBorder = 5;
    x_bgColor = Qt::white;
    x_nRatio = 5;


    top=new QWidget(this);
    top->setStyleSheet("background:#333");
    top->resize(this->width()-10,40);
    top->move(x_nBorder,x_nBorder);
    title=new QLabel(top);
    title->setText("曲大家远程控制软件");
    title->setStyleSheet("color:#fff;");
    QFont ft("黑体",12,1);
    title->setFont(ft);
    title->move(40,10);
    closeBtn=new QPushButton(top);
    closeBtn->setStyleSheet("QPushButton{border:none;background:#f00 url(:/images/close.png) center no-repeat;}QPushButton::hover{background:#900 url(:/images/close.png) center no-repeat;}");
    closeBtn->resize(25,25);
    closeBtn->move(top->width()-32,10);
    QPixmap map(":/images/tlogo.png");
    map=map.scaled(24,24);
    ico=new QLabel(top);
    ico->setPixmap(map);
    ico->move(5,5);
    connect(closeBtn, SIGNAL(clicked()), this, SLOT(doClose()));//连接关闭事件

    this->setTitle("曲大家远程控制软件");
}

MDialog::~MDialog()
{

}
void MDialog::show(){

    setWindowState(Qt::WindowNoState);
    QDialog::show();

}

void MDialog::paintEvent(QPaintEvent *event)
{

        if(this->top!=NULL){//重置标题大小
             QRect rect = this->rect();
            top->resize(rect.width()-x_nBorder*2,38);

            closeBtn->move(top->width()-30,6);

        }

        QPainter painter(this);

            painter.setRenderHint(QPainter::Antialiasing);

            drawBG(&painter);

            drawShadow(&painter);



        QWidget::paintEvent(event);

}
void MDialog::drawBG(QPainter* painter)
{
    painter->save();
    QBrush brush(x_bgColor);
    painter->setBrush(brush);
    painter->setPen(Qt::NoPen);

    QRect _rect = this->rect();
    QRect _rectBG = QRect(_rect.x() + x_nBorder, _rect.y() + x_nBorder, _rect.width() - 2 * x_nBorder, _rect.height() - 2 * x_nBorder);

    painter->drawRoundedRect(_rectBG, x_nRatio, x_nRatio);
    painter->restore();
}

void MDialog::drawShadow(QPainter* painter)
{
    painter->save();

    QColor color(0, 0, 0, 0);

    for (int i = 0; i < x_nBorder; ++i)
    {
        QPainterPath path;
        path.setFillRule(Qt::WindingFill);

        path.addRoundedRect(x_nBorder - i, x_nBorder - i, this->width() - (x_nBorder - i) * 2, this->height() - (x_nBorder - i) * 2, x_nRatio, x_nRatio);

        color.setAlpha(100 - qSqrt(i) * 50);
        painter->setPen(color);
        painter->drawPath(path);
    }

    painter->restore();
}
void MDialog::hideTitle(){
    top->hide();
}
void MDialog::hideMin(){
    //minBtn->hide();
}
void MDialog::hideIcon(){
    ico->hide();
    title->move(10,10);
}
void MDialog::hideMenu(){
   // menuBtn->close();
}
void MDialog::hideClose(){
    closeBtn->hide();
}
void MDialog::setTitle(QString title)
{
   // echo("title="+title);
    this->setWindowTitle(title);
    this->title->setText(title);
}
void MDialog::doClose(){


   QApplication::exit();
}
void MDialog::doMenu(){

}
void MDialog::doMin(){

   setWindowState(Qt::WindowMinimized);
}
void MDialog::mousePressEvent(QMouseEvent* event)
{
    if(event->button() == Qt::LeftButton && event->y()<=40)
    {
        isPressed = true;
        old = event->globalPos();
    }
}

void MDialog::mouseMoveEvent(QMouseEvent* event)
{
    if(isPressed && event->buttons().testFlag(Qt::LeftButton))
    {
        this->move(this->pos() + (event->globalPos() - old));
        old = event->globalPos();
    }
}

void MDialog::mouseReleaseEvent(QMouseEvent* event)
{
    if(event->button() == Qt::LeftButton)
    {
        isPressed = false;
    }
}

你可能感兴趣的:(qt,java,qt,java)