PDA

View Full Version : Transparent top-level window



Vanir
1st December 2007, 00:43
Hi,

I need to have a top-level window/widget that displays text scrolling across the desktop screen. The background/base must be able to be set so that it is transparent or opaque with a solid colour.

I have searched long and hard to and tried innumerable pieces of code to find how to achieve a transparent QWidget. ie One that does not display a default grey background or base!

It must be said that this seems a not an uncommon requirement but finding any useful documentation on the issue is very much less common.

Would anyone care to enlighten this frustrated newbie in Qt 4.3.2?

A small complete example would be very much appreciated!

Something structured like my attempt which does not work.


#include <QtGui/QApplication>
#include "qt_testapp.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
qt_testApp w;
w.show();
a.connect(&a, SIGNAL(lastWindowClosed()), &a, SLOT(quit()));
return a.exec();
}



#ifndef QT_TESTAPP_H
#define QT_TESTAPP_H

#include <QtGui/QMainWindow>
#include "ui_qt_testapp.h"

class MyTextWidget;

class qt_testApp : public QMainWindow
{
Q_OBJECT

public:
qt_testApp(QWidget *parent = 0, Qt::WFlags flags = 0);
~qt_testApp();

private:
Ui::qt_testAppClass ui;

private slots:
void on_pushButton_clicked();

private:
MyTextWidget * m_pTextWidget;


};

#endif // QT_TESTAPP_H


#include "qt_testapp.h"
#include "mytextwidget.h"

qt_testApp::qt_testApp(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags), m_pTextWidget(0)
{
ui.setupUi(this);
}

qt_testApp::~qt_testApp()
{

}


void qt_testApp::on_pushButton_clicked()
{
if(NULL == m_pTextWidget)
{
m_pTextWidget = new MyTextWidget;
//m_pTextWidget->setBackgroundRole(QPalette::NoRole);
//QPalette p = m_pTextWidget->palette();
//p.setBrush(QPalette::Base, Qt::transparent);
//m_pTextWidget->setPalette(p);
}

m_pTextWidget->show();
}


// mytextwidget.h created on Fri Nov 30 18:45:00 UTC 2007
#ifndef MYTEXTWIDGET_H_AF06570D_B36E_4B82_8F97_C456AF4A38F D
#define MYTEXTWIDGET_H_AF06570D_B36E_4B82_8F97_C456AF4A38F D

#include <QWidget>
#include "ui_mytextwidget.h"

using namespace Ui;

class MyTextWidget : public QWidget, public MyTextWidgetClass
{
Q_OBJECT

public:
MyTextWidget(QWidget *parent = 0);
~MyTextWidget();
private:
virtual void paintEvent(QPaintEvent * event);
};

#endif // MYTEXTWIDGET_H_AF06570D_B36E_4B82_8F97_C456AF4A38F D


// mytextwidget.cpp created on Fri Nov 30 18:45:00 UTC 2007

#include <QPainter>
#include <QBitmap>
#include "mytextwidget.h"

MyTextWidget::MyTextWidget(QWidget *parent)
: QWidget(parent/*, Qt::FramelessWindowHint*/)
{
setupUi(this);
}

MyTextWidget::~MyTextWidget()
{

}

void MyTextWidget::paintEvent(QPaintEvent * event)
{
QWidget::paintEvent(event);
QPixmap pm(size());

pm.fill(QColor(0,0,0,0));

QBitmap bm(pm);


QPainter p(&pm);
}

Thanks

Vanir

wysota
1st December 2007, 03:34
QWidget::windowOpacity is something that should work, but if it doesn't work as you'd expect it to, you have to do some platform dependent code writing. For X11 it's enough to use ARGB windows, for Windows there isn't a straightforward solution but you might look arouund the forum, there are threads on the same subject that contain solutions. For Mac - I don't know, never tried.

Vanir
1st December 2007, 09:51
Wysota,

I am developing cross-platform code on a WinXP VS2005 box.

QWidget::windowOpacity does not meet the requirement, it does the whole window and everything that is drawn inside it; any drawn text will also be affected.

I am sure it can be done. As you suggest it may have to break the cross-platform criteria which if true does suggest a minor deficiency in Qt.

Thanks

Vanir

wysota
1st December 2007, 12:19
It can be done only if the underlying system supports it and the code is heavily OS release dependent - you need different code not only on X11 and Win32, but probably also on Vista and XP or 2k. On earlier Windows systems it can't be done at all (at least without some dirty hacks). I'm sure eventually we'll get support for that in Qt, but I wouldn't bet any money it will be any time soon.