1.如何制作使得无边框窗体的边框为圆角
2.如何将无边框窗体中的关闭,最大,最小化按钮放置在最顶端,我现在做出的效果是离顶端还有一段距离。
以上两个问题麻烦大侠支个招,嘿嘿,谢谢啦,祝各位新年快乐。
------解决方案--------------------
圆角的我也想知道
至于第二个问题是否可以不用它自带的那三个按钮自己实现这三个按钮呢
------解决方案--------------------
QStyleSheet
------解决方案--------------------
来点代码,胜过千言万语:
- C/C++ code
widget.h头文件 #ifndef WIDGET_H #define WIDGET_H #include <QtGui/QWidget> #include <qpoint.h> class QPushButton; class QPoint; class Widget : public QWidget { Q_OBJECT public: Widget(QWidget *parent = 0); ~Widget(); private: QPushButton *closeButton; bool isMouseDown; QPoint oldLocation; virtual void mouseMoveEvent(QMouseEvent *); virtual void mousePressEvent(QMouseEvent *); virtual void mouseReleaseEvent(QMouseEvent *); private slots: void on_closeButton_clicked(); }; #endif // WIDGET_H Widget.cpp源文件: #include "widget.h" #include <QPushButton> #include <QBitmap> #include <QPainter> #include <QMouseEvent> #include <qdebug.h> Widget::Widget(QWidget *parent) : QWidget(parent) { setWindowFlags(Qt::FramelessWindowHint);//无边框窗体 closeButton = new QPushButton("X",this); closeButton->setObjectName("closeButton"); closeButton->setGeometry(15,0,22,22); QMetaObject::connectSlotsByName(this); resize(300,300); QBitmap bmp(this->size()); bmp.fill(); QPainter p(&bmp); p.setPen(Qt::NoPen); p.setBrush(Qt::black); p.drawRoundedRect(bmp.rect(),10,10); setMask(bmp);//设置窗体遮罩 isMouseDown = false; } void Widget::mousePressEvent(QMouseEvent *e) { isMouseDown = true; oldLocation = this->mapToGlobal(e->pos()); qDebug()<<oldLocation; } void Widget::mouseReleaseEvent(QMouseEvent *) { isMouseDown = false; } void Widget::mouseMoveEvent(QMouseEvent *e) { if(isMouseDown)//如果鼠标左键按下 { QPoint p = this->mapToGlobal(e->pos()); int x = p.x() - oldLocation.x(); int y = p.y() - oldLocation.y(); oldLocation = p; p = this->pos(); p.setX(p.x() + x); p.setY(p.y() + y); this->move(p);//移动窗体 } } Widget::~Widget() { } void Widget::on_closeButton_clicked() { this->close(); } main.cpp文件: #include <QtGui/QApplication> #include "widget.h" int main(int argc, char *argv[]) { QApplication a(argc, argv); Widget w; w.show(); return a.exec(); }
------解决方案--------------------
看范例文档
------解决方案--------------------
都可以通过QStyleSheet实现