Qt 背景透明、无边框标题栏、鼠标拖动、定时器、获取屏幕分辨率,设置初始位置

来源:互联网 发布:电信网络优化工程师 编辑:程序博客网 时间:2024/06/08 04:46
  .cpp

DigiClock::DigiClock(QWidget*parent) :

    QLCDNumber(parent)////////////////////////注意继承的类~
{

    /*QPalette p=palette();
    p.setColor(QPalette::Background,QColor(0x00,0xff,0x00,0x00));
    setPalette(p);*/

    setWindowOpacity(1);
    setWindowFlags(Qt::FramelessWindowHint);//这个是widget的标题栏和边框去掉
    setAttribute(Qt::WA_TranslucentBackground);//这个是widget的背景弄透明
    QDesktopWidget* pDw = QApplication::desktop();//获得桌面窗体
    QRect rect = pDw->screenGeometry();//获得分辨率
    move(rect.width()/2-175,rect.height()/10*9);//move就是是设置位置的是widget的位置!
    resize(350,80);
    setLineWidth(0);//这边的边框是Qframe的边框弄没     这个很关键!!!要注意Qwidget有边框,Qframe也有个边框将厚度设成0就等于隐藏了
    setSegmentStyle(QLCDNumber::Filled);//lcd内部的字体样式

    setDigitCount(8);
    QTimer *timer=new QTimer(this);
    connect(timer,SIGNAL(timeout()),this,SLOT(showTime()));
    timer->start(1000);////////////////////////////////////////////////////////////1秒触发一次timeout()
    showTime();
}

void DigiClock::showTime()
{
    QTime time=QTime::currentTime();
    text=time.toString();
    display(text);
}
void DigiClock::mousePressEvent(QMouseEvent *event)
{
    if(event->button()==Qt::LeftButton)
    {
        dragPosition=event->globalPos()-frameGeometry().topLeft();
        event->accept();
    }
    if(event->button()==Qt::RightButton)
    {
        exit(0);//此处必须写exit不用用close!!!exit是application的关闭,而close只是frame的关闭
    }
}
void DigiClock::mouseMoveEvent(QMouseEvent *event)
{
    if(event->buttons()&Qt::LeftButton)
    {
        move(event->globalPos()-dragPosition);
        event->accept();
    }
}
.h

class DigiClock : public QLCDNumber
{
    Q_OBJECT
public:
    explicit DigiClock(QWidget *parent = 0);
    void mousePressEvent(QMouseEvent *event);
    void mouseMoveEvent(QMouseEvent *event);
signals:
public slots:
    void showTime();
private:
    QPoint dragPosition;
    QString text;
};
0 0