我很感激帮助在Qt中创建具有以下特征的顶级窗口。窗口必须是:
基本上,我正在尝试将我们的QT应用程序折叠到桌面顶部边框上的顶级图标。
答案 0 :(得分:4)
您将在Making a borderless window with for Qt中找到第一部分的答案,并在Select & moving Qwidget in the screen中找到第二部分的答案。
将两者结合起来,并添加最后一部分很简单。
以下是你如何做到的:
#include <QtGui>
class W: public QWidget
{
Q_OBJECT
使用几个按钮设置无边框小部件以锁定/解锁并退出:
public:
W(QWidget *parent=0)
: QWidget(parent, Qt::FramelessWindowHint), locked(false)
{
QPushButton *lock = new QPushButton("Lock");
QPushButton *unlock = new QPushButton("Unlock");
QPushButton *quit = new QPushButton("&Quit");
connect(lock, SIGNAL(clicked()), this, SLOT(lock()));
connect(unlock, SIGNAL(clicked()), this, SLOT(unlock()));
connect(quit, SIGNAL(clicked()),
QApplication::instance(), SLOT(quit()));
QHBoxLayout *l = new QHBoxLayout;
l->addWidget(lock);
l->addWidget(unlock);
l->addWidget(quit);
setLayout(l);
}
public slots:
void lock() {
locked = true;
move(x(), 0); // move window to the top of the screen
}
void unlock() { locked = false; }
执行鼠标操作:
protected:
void mousePressEvent(QMouseEvent *evt)
{
oldPos = evt->globalPos();
}
void mouseMoveEvent(QMouseEvent *evt)
{
const QPoint delta = evt->globalPos() - oldPos;
if (locked)
// if locked, ignore delta on y axis, stay at the top
move(x()+delta.x(), y());
else
move(x()+delta.x(), y()+delta.y());
oldPos = evt->globalPos();
}
private:
bool locked;
QPoint oldPos;
};