• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            逛奔的蝸牛

            我不聰明,但我會很努力

               ::  :: 新隨筆 ::  ::  :: 管理 ::

            The titlebar belongs to the OS and we don't have control over that one. You can create your own titlebar, but note that this requires some work. In order to create your own titlebar then make a QWidget subclass that contains three toolbuttons that handle the close, minimize and maximize events in addition to the moving of the window. Then make a QFrame subclass which does not have a titlebar provided via the window system. This is done by setting theQt::FramelessWindowHint window flag, however this will make it impossible to resize or move the window via the window system. What can be done is you can add your custom titlbar as a private member to the frame and add the it first to the frame's vertical layout. The frame also needs a content widget which allows widgets to be added to it. Finally the QFrame subclass needs to reimplement the mouse events to handle the resizing and moving of the window. The example below demonstrates how this can be achieved.

            #include <QApplication>

            #include <QtGui>

            #include <QLayout>

            #include <QStyle>


            class TitleBar : public QWidget {

                Q_OBJECT

            public:

                TitleBar(QWidget *parent) {

                    // Don't let this widget inherit the parent's backround color

                    setAutoFillBackground(true);

                    // Use a brush with a Highlight color role to render the background 

                    setBackgroundRole(QPalette::Highlight);

                    

                    minimize = new QToolButton(this);

                    maximize = new QToolButton(this);

                    close= new QToolButton(this);

                    

                    // Use the style to set the button pixmaps

                    QPixmap pix = style()->standardPixmap(QStyle::SP_TitleBarCloseButton);

                    close->setIcon(pix);

                    

                    maxPix = style()->standardPixmap(QStyle::SP_TitleBarMaxButton);

                    maximize->setIcon(maxPix);

                    

                    pix = style()->standardPixmap(QStyle::SP_TitleBarMinButton);

                    minimize->setIcon(pix);

                    

                    restorePix = style()->standardPixmap(QStyle::SP_TitleBarNormalButton);

                    

                    minimize->setMinimumHeight(20);

                    close->setMinimumHeight(20);

                    maximize->setMinimumHeight(20);

                    

                    

                    QLabel *label = new QLabel(this);

                    label->setText("Window Title");

                    parent->setWindowTitle("Window Title");

                    

                    QHBoxLayout *hbox = new QHBoxLayout(this);

                    

                    hbox->addWidget(label);

                    hbox->addWidget(minimize);

                    hbox->addWidget(maximize);

                    hbox->addWidget(close);

                    

                    hbox->insertStretch(1, 500);

                    hbox->setSpacing(0);

                    setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);

                    

                    maxNormal = false;

                    

                    connect(close, SIGNAL( clicked() ), parent, SLOT(close() ) );

                    connect(minimize, SIGNAL( clicked() ), this, SLOT(showSmall() ) );

                    connect(maximize, SIGNAL( clicked() ), this, SLOT(showMaxRestore() ) );

                }

                

            public slots:

                void showSmall() {

                    parentWidget()->showMinimized();

                }

                

                void showMaxRestore() {

                    if (maxNormal) {

                        parentWidget()->showNormal();

                        maxNormal = !maxNormal;

                        maximize->setIcon(maxPix);

                    } else {

                        parentWidget()->showMaximized();

                        maxNormal = !maxNormal;

                        maximize->setIcon(restorePix);

                    }

                }

            protected:

                void mousePressEvent(QMouseEvent *me) {

                    startPos = me->globalPos();

                    clickPos = mapToParent(me->pos());

                }

                void mouseMoveEvent(QMouseEvent *me) {

                    if (maxNormal)

                        return;

                    parentWidget()->move(me->globalPos() - clickPos);

                }

                

            private:

                QToolButton *minimize;

                QToolButton *maximize;

                QToolButton *close;

                QPixmap restorePix, maxPix;

                bool maxNormal;

                QPoint startPos;

                QPoint clickPos;

            };


            class Frame : public QFrame {

            public:

                

                Frame() {

                    m_mouse_down = false;

                    setFrameShape(Panel);

                    

                    // Make this a borderless window which can't

                    // be resized or moved via the window system

                    setWindowFlags(Qt::FramelessWindowHint);

                    setMouseTracking(true);

                    

                    m_titleBar = new TitleBar(this);

                    

                    m_content = new QWidget(this);

                    

                    QVBoxLayout *vbox = new QVBoxLayout(this);

                    vbox->addWidget(m_titleBar);

                    vbox->setMargin(0);

                    vbox->setSpacing(0);

                    

                    QVBoxLayout *layout = new QVBoxLayout(this);

                    layout->addWidget(m_content);

                    layout->setMargin(5);

                    layout->setSpacing(0);

                    vbox->addLayout(layout);

                }

                

                // Allows you to access the content area of the frame

                // where widgets and layouts can be added

                QWidget *contentWidget() const { return m_content; }

                

                TitleBar *titleBar() const { return m_titleBar; }

                

                void mousePressEvent(QMouseEvent *e) {

                    m_old_pos = e->pos();

                    m_mouse_down = e->button() == Qt::LeftButton;

                }

                

                void mouseMoveEvent(QMouseEvent *e) {

                    int x = e->x();

                    int y = e->y();

                    

                    if (m_mouse_down) {

                        int dx = x - m_old_pos.x();

                        int dy = y - m_old_pos.y();

                        

                        QRect g = geometry();

                        

                        if (left)

                            g.setLeft(g.left() + dx);

                        if (right)

                            g.setRight(g.right() + dx);

                        if (bottom)

                            g.setBottom(g.bottom() + dy);

                        

                        setGeometry(g);

                        

                        m_old_pos = QPoint(!left ? e->x() : m_old_pos.x(), e->y());

                    } else {

                        QRect r = rect();

                        left = qAbs(x - r.left()) <= 5;

                        right = qAbs(x - r.right()) <= 5;

                        bottom = qAbs(y - r.bottom()) <= 5;

                        bool hor = left | right;

                        

                        if (hor && bottom) {

                            if (left)

                                setCursor(Qt::SizeBDiagCursor);

                            else 

                                setCursor(Qt::SizeFDiagCursor);

                        } else if (hor) {

                            setCursor(Qt::SizeHorCursor);

                        } else if (bottom) {

                            setCursor(Qt::SizeVerCursor);

                        } else {

                            setCursor(Qt::ArrowCursor);

                        }

                    }

                }

                

                void mouseReleaseEvent(QMouseEvent *e) {

                    m_mouse_down = false;

                }

                

            private:

                TitleBar *m_titleBar;

                QWidget *m_content;

                QPoint m_old_pos;

                bool m_mouse_down;

                bool left, right, bottom;

            };



            #include "main.moc"


            int main(int argc, char **argv) {

                QApplication app(argc, argv);

                

                Frame box;

                box.move(0,0);

                

                QVBoxLayout *l = new QVBoxLayout(box.contentWidget());

                l->setMargin(0);

                QTextEdit *edit = new QTextEdit(box.contentWidget());

                l->addWidget(edit);

                

                box.show();

                return app.exec();    

            }


            From: http://qt.nokia.com/developer/faqs/535

            posted on 2009-11-07 17:37 逛奔的蝸牛 閱讀(1865) 評論(0)  編輯 收藏 引用 所屬分類: Qt
            久久精品一区二区| 国产毛片欧美毛片久久久| 久久国产精品-久久精品| 99久久人人爽亚洲精品美女| 久久91这里精品国产2020| 欧美激情精品久久久久久| 99久久夜色精品国产网站| 国产精品一区二区久久不卡| 免费观看成人久久网免费观看| 精品国产乱码久久久久久浪潮| 思思久久99热只有频精品66| 99久久免费国产精品特黄| 蜜臀久久99精品久久久久久小说| 午夜不卡888久久| 亚洲精品午夜国产VA久久成人| 久久99精品国产99久久6男男| 日韩欧美亚洲综合久久影院Ds| 久久久久久国产精品免费无码| 久久国产午夜精品一区二区三区| 亚洲AV无码久久寂寞少妇| 欧美一级久久久久久久大片| 久久99精品国产麻豆宅宅| 久久久久免费精品国产| 欧美激情精品久久久久久久九九九 | 国产精品久久新婚兰兰| 久久香蕉国产线看观看99| 性欧美大战久久久久久久久 | 久久精品男人影院| 久久精品国产久精国产思思| 久久精品中文字幕大胸| 久久亚洲中文字幕精品一区四| 四虎国产永久免费久久| 久久综合久久久| 欧美亚洲另类久久综合| 热久久这里只有精品| 亚洲国产精品久久久久网站| 久久精品国产只有精品2020| 97久久精品人人做人人爽| 日本道色综合久久影院| 91久久精品国产91性色也| 久久九九久精品国产免费直播|