QT:不規則視窗的實現

來源:互聯網
上載者:User

主要思路:
1:將表單設為Qt::FramelessWindowHint(去掉標題列)。
2:用一幅有部分地區是透明的圖片作為程式的介面,並將圖片透明的地方設為穿透。

3:重載程式的滑鼠事件。

運行時(淺綠色的是案頭背景)

原始碼:

#include <QtGui>  class IrregularWidget : public QWidget  {  Q_OBJECT  public:  IrregularWidget(QWidget *parent = 0);  protected:  void mousePressEvent(QMouseEvent *event);  void mouseMoveEvent(QMouseEvent *event);  void paintEvent(QPaintEvent *event);  void enterEvent(QEvent *event);  void leaveEvent(QEvent *event);  private:  QPoint m_CurrentPos;  QPixmap m_Pixmap;  };  IrregularWidget::IrregularWidget(QWidget *parent)  : QWidget(parent, Qt::FramelessWindowHint)  {  setWindowTitle("Irregular widget");  //載入一幅有部分地區是透明的圖片作為程式的介面  m_Pixmap.load("delete.png");  resize( m_Pixmap.size() );  //不規則視窗的關鍵,將圖片透明的地方設為穿透  setMask( m_Pixmap.mask() );  }  void IrregularWidget::mousePressEvent(QMouseEvent *event)  {  //按住左鍵可以托動視窗,按下右鍵關閉程式  if(event->button() == Qt::LeftButton)  {  m_CurrentPos = event->globalPos() - frameGeometry().topLeft();  event->accept();  }  else if(event->button() == Qt::RightButton)  close();  }  void IrregularWidget::mouseMoveEvent(QMouseEvent *event)  {  if (event->buttons() && Qt::LeftButton)  {  move(event->globalPos() - m_CurrentPos);  event->accept();  }  }  void IrregularWidget::paintEvent(QPaintEvent *event)  {  QPainter painter(this);  painter.drawPixmap(0, 0, m_Pixmap);  }  void IrregularWidget::leaveEvent(QEvent *event)  {  //滑鼠離開視窗時是普通的指標  setCursor(Qt::ArrowCursor);  }  void IrregularWidget::enterEvent(QEvent *event)  {  //滑鼠留在視窗上時是一個手指  setCursor(Qt::PointingHandCursor);  }  #include "main.moc"  int main(int argc, char *argv[])  {  QApplication app(argc, argv);  IrregularWidget *widget = new IrregularWidget;  widget->show();  return app.exec();  }  

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.