Translucent QWidget should be clickable cross-os

谁说胖子不能爱 提交于 2021-01-29 03:36:34

问题


I'm trying to accomplish the following with a Qt (C++) app, cross OS. Upon running the program a new window pops up, a fullscreen QWidget. Now I want this to be see-through/transparent, so the user won't actually see it. On this 'layer' a user can drag his/her mouse to draw a (red) rectangle to select an area which is - when the mouse is released - taken a screenshot of.

The problem:
The issue lays in the whole transparent layer thing since this doesn't appear to work well cross OS. Because when I click on where the layer is, to invoke the mousePressEvent(), I click through it on to the window below it as if it isn't even there. On Ubuntu, however, I do not. I want the same effect on Windows, but thus far I got nothing...
(Making another GUI object appear, such as button, would only make the button a clickable part of the layer)

Tested on Ubuntu 11.04 32-bit and Windows 7 Home Premium 64-bit. (Trying to get around to a Mac one, would this issue be solved.)

So does anyone know how this would work? I've included my code thus far. (Clearing out all my 100 other attempts.)

main.cpp Here I set the translucentBackground, here I probably miss a setting or something is not configured right.

#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();
}

widget.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);
    }

widget.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

Also, I think I've gone through every result Google will find on this subject, same as the API docs of Qt. I've seriously run out of options for this one. (I started this project in Java, but C++ with Qt seems to be, thus far, far less work.)

Any help would very much be appreciated!


回答1:


This is a complete and total hack but it's the only way that I know of to do it. Take a screenshot of the screen and then use that as your widget's background, making sure that the right piece of the screen shows in the window, especially on resizes or moves, etc. At least it's cross platform.

However, it was also the way that KDE 3's fake transparency was implemented before such things like ARGB visuals and what not existed in X11.



来源:https://stackoverflow.com/questions/6945934/translucent-qwidget-should-be-clickable-cross-os

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!