我正在尝试使用跨操作系统的 Qt (C++) 应用程序完成以下任务。运行程序后会弹出一个新窗口,即全屏 QWidget。现在我希望它是透明/透明的,所以用户实际上不会看到它。在这个“层”上,用户可以拖动他/她的鼠标来绘制一个(红色)矩形来选择一个区域——当鼠标被释放时——截屏。
问题:
问题在于整个透明层,因为这似乎在跨操作系统时效果不佳。因为当我单击图层所在的位置时,为了调用 mousePressEvent(),我单击它到它下面的窗口,就好像它甚至不存在一样。然而,在 Ubuntu 上,我没有。我希望在 Windows 上获得相同的效果,但到目前为止我什么也没得到......
(出现另一个 GUI 对象,例如按钮,只会使按钮成为图层的可点击部分)
在 Ubuntu 11.04 32 位和 Windows 7 Home Premium 64 位上测试。(尝试使用Mac,这个问题会解决吗?)
那么有谁知道这将如何工作? 到目前为止,我已经包含了我的代码。(清除我所有的其他 100 次尝试。)
main.cpp 这里我设置了translucentBackground,这里我可能错过了一个设置或者配置不正确。
#include <QtGui/QApplication>
#include "widget.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
//Fullscreen app
w.showFullScreen();
w.setAttribute(Qt::WA_NoSystemBackground);
w.setAttribute(Qt::WA_TranslucentBackground);
w.setMouseTracking(true);
w.setWindowFlags(Qt::Window | Qt::CustomizeWindowHint | Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint);
//Show
w.show();
return a.exec();
}
小部件.cpp
#include "widget.h"
#include "ui_widget.h"
#include "QDebug"
Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget)
{
this->setWindowFlags(Qt::CustomizeWindowHint | Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint);
QPalette palette(Widget::palette());
palette.setColor(backgroundRole(), Qt::white);
setPalette(palette);
this->clicked = false;
ui->setupUi(this);
}
Widget::~Widget()
{
delete ui;
}
void Widget::mousePressEvent ( QMouseEvent * event )
{
//Record start
if (event->button() == Qt::LeftButton){
x = event->globalX();
y = event->globalY();
this->clicked = true;
width = 0;
height = 0;
}
}
void Widget::mouseMoveEvent ( QMouseEvent * event )
{
if (this->clicked == true){
int x2 = event->globalX();
int y2 = event->globalY();
if(x < x2){
width = x2 - x;
}else{
width = x - x2;
//Resetting startpoint when dragging to the left side on your screen, copy from java so this doesn't actually works yet.
x = x - width-2;
}
if(y < y2){
height = y2 - y;
}else{
height = y - y2;
//Resetting startpoint when dragging to the left side on your screen, copy from java so this doesn't actually works yet.
y = y - height-2;
}
//Redraw rectangle
update();
qDebug("wa");
}
}
void Widget::mouseReleaseEvent ( QMouseEvent * event )
{
if (event->button() == Qt::LeftButton)
{
//Record end
qDebug("-------");
this->clicked = false;
}
}
void Widget::paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
painter.setPen(Qt::red);
painter.drawRect(x,y,width,height);
}
小部件.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QtGui>
#include<QMouseEvent>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
private:
Ui::Widget *ui;
bool clicked;
int x,y,width,height;
void mousePressEvent ( QMouseEvent * event );
void mouseMoveEvent ( QMouseEvent * event );
void mouseReleaseEvent ( QMouseEvent * event );
protected:
void paintEvent(QPaintEvent *);
};
#endif // WIDGET_H
另外,我想我已经浏览了谷歌会在这个主题上找到的所有结果,就像 Qt 的 API 文档一样。我已经严重用完了这个选项。(我在 Java 中开始了这个项目,但到目前为止,使用 Qt 的 C++ 似乎工作量要少得多。)
任何帮助将不胜感激!