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