<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                本系列所有文章可以在這里查看[http://blog.csdn.net/cloud_castle/article/category/2123873](http://blog.csdn.net/cloud_castle/article/category/2123873) 這個例子應該說是在上一個例子的基礎上擴展了一些功能,擴展了什么呢?看看介紹: The Shaped Clock example shows how to apply a widget mask to a top-level widget to produce a shaped window. ![](https://box.kancloud.cn/2016-01-18_569cbd0127db4.jpg) Widget masks are used to customize the shapes of top-level widgets by restricting the available area for painting. On some window systems, setting certain window flags will cause the window decoration (title bar, window frame, buttons) to be disabled, allowing specially-shaped windows to be created. In this example, we use this feature to create a circular window containing an analog clock. Since this example's window does not provide a File menu or a close button, we provide a context menu with an Exit entry so that the example can be closed. Click the right mouse button over the window to open this menu. 介紹很長,主要也就是告訴我們他用window flags隱藏了邊框,從而創建了一個特殊形狀的窗口,因為沒有關閉鍵,所以提供了關閉菜單。 看看代碼: shapedclock.h: ~~~ #ifndef SHAPEDCLOCK_H #define SHAPEDCLOCK_H #include <QWidget> //! [0] class ShapedClock : public QWidget { Q_OBJECT public: ShapedClock(QWidget *parent = 0); QSize sizeHint() const; // 重寫了sizeHint(),常成員函數,不會改變成員的值,其內部也不能調用非const函數 protected: void mouseMoveEvent(QMouseEvent *event); // 相比<span style="font-family: Arial, Helvetica, sans-serif;">Analog Clock Example,</span><span style="font-family: Arial, Helvetica, sans-serif;">增加了幾個事件處理函數</span> void mousePressEvent(QMouseEvent *event); void paintEvent(QPaintEvent *event); void resizeEvent(QResizeEvent *event); private: QPoint dragPosition; // 一個全局的QPoint用來處理拖放事件 }; //! [0] #endif ~~~ 再來看實現代碼: shapedclock.cpp: ~~~ #include <QtWidgets> #include "shapedclock.h" //! [0] ShapedClock::ShapedClock(QWidget *parent) : QWidget(parent, Qt::FramelessWindowHint | Qt::WindowSystemMenuHint) // 其實我們不用setWindowsFlag()那么麻煩,放在初始化列表就可以了。第一個參數表明此窗口無框架,第二個添加了一個窗口系統菜單。 { QTimer *timer = new QTimer(this); connect(timer, SIGNAL(timeout()), this, SLOT(update())); timer->start(1000); QAction *quitAction = new QAction(tr("E&xit"), this); // 創建響應退出事件 quitAction->setShortcut(tr("Ctrl+Q")); // 設置快捷鍵Ctrl+Q connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit())); addAction(quitAction); setContextMenuPolicy(Qt::ActionsContextMenu); // 設置菜單顯示策略,這里當actions()觸發時顯示 setToolTip(tr("Drag the clock with the left mouse button.\n" // 對整個窗口設置ToolTip "Use the right mouse button to open a context menu.")); setWindowTitle(tr("Shaped Analog Clock")); } //! [0] //! [1] void ShapedClock::mousePressEvent(QMouseEvent *event) { if (event->button() == Qt::LeftButton) { dragPosition = event->globalPos() - frameGeometry().topLeft(); // 由鼠標相對屏幕的坐標減去窗口左上角坐標,其實得到的就是鼠標相對窗口的位置 event->accept(); // 盡管我們可以不這樣做,但這是個好習慣。告訴Qt這個事件已經接收,不用再向我的父對象發送了。在這個例子中我們當然不希望拖動時鐘時把主窗口一同拖動了吧。 } } //! [1] //! [2] void ShapedClock::mouseMoveEvent(QMouseEvent *event) { if (event->buttons() & Qt::LeftButton) { move(event->globalPos() - dragPosition); // 我們把上面的式子被減數和結果換個位子<span style="font-family: Arial, Helvetica, sans-serif;">就知道這個move的意思啦</span> event->accept(); } } //! [2] //! [3] void ShapedClock::paintEvent(QPaintEvent *) // paint函數可以參見Analog Clock Example { static const QPoint hourHand[3] = { QPoint(7, 8), QPoint(-7, 8), QPoint(0, -40) }; static const QPoint minuteHand[3] = { QPoint(7, 8), QPoint(-7, 8), QPoint(0, -70) }; QColor hourColor(127, 0, 127); QColor minuteColor(0, 127, 127, 191); int side = qMin(width(), height()); QTime time = QTime::currentTime(); QPainter painter(this); painter.setRenderHint(QPainter::Antialiasing); painter.translate(width() / 2, height() / 2); painter.scale(side / 200.0, side / 200.0); painter.setPen(Qt::NoPen); painter.setBrush(hourColor); painter.save(); painter.rotate(30.0 * ((time.hour() + time.minute() / 60.0))); painter.drawConvexPolygon(hourHand, 3); painter.restore(); painter.setPen(hourColor); for (int i = 0; i < 12; ++i) { painter.drawLine(88, 0, 96, 0); painter.rotate(30.0); } painter.setPen(Qt::NoPen); painter.setBrush(minuteColor); painter.save(); painter.rotate(6.0 * (time.minute() + time.second() / 60.0)); painter.drawConvexPolygon(minuteHand, 3); painter.restore(); painter.setPen(minuteColor); for (int j = 0; j < 60; ++j) { if ((j % 5) != 0) painter.drawLine(92, 0, 96, 0); painter.rotate(6.0); } } //! [3] //! [4] void ShapedClock::resizeEvent(QResizeEvent * /* event */) // 與paintEvent一樣,該事件在窗口創建時也會被調用一次,Mask創建在這個事件保證Mask區域能被準備更新 { int side = qMin(width(), height()); QRegion maskedRegion(width() / 2 - side / 2, height() / 2 - side / 2, side, // 使用QRegion選擇了一時鐘的圓形區域 side, QRegion::Ellipse); setMask(maskedRegion); // 由setMask創建了這個"不規則窗口",而該窗口只會在可視區域相應鼠標事件 } //! [4] //! [5] QSize ShapedClock::sizeHint() const // 這個函數的返回值將作為Widget大小的一個參考,之所以說參考,當窗體內有布局和控件時,將優先考慮它們的size和sizePolicy { return QSize(100, 100); } //! [5] ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看