Results 1 to 4 of 4

Thread: Transparent top-level window

  1. #1
    Join Date
    Nov 2007
    Posts
    12
    Thanks
    2
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Transparent top-level window

    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.

    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "qt_testapp.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. qt_testApp w;
    8. w.show();
    9. a.connect(&a, SIGNAL(lastWindowClosed()), &a, SLOT(quit()));
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. #ifndef QT_TESTAPP_H
    2. #define QT_TESTAPP_H
    3.  
    4. #include <QtGui/QMainWindow>
    5. #include "ui_qt_testapp.h"
    6.  
    7. class MyTextWidget;
    8.  
    9. class qt_testApp : public QMainWindow
    10. {
    11. Q_OBJECT
    12.  
    13. public:
    14. qt_testApp(QWidget *parent = 0, Qt::WFlags flags = 0);
    15. ~qt_testApp();
    16.  
    17. private:
    18. Ui::qt_testAppClass ui;
    19.  
    20. private slots:
    21. void on_pushButton_clicked();
    22.  
    23. private:
    24. MyTextWidget * m_pTextWidget;
    25.  
    26.  
    27. };
    28.  
    29. #endif // QT_TESTAPP_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "qt_testapp.h"
    2. #include "mytextwidget.h"
    3.  
    4. qt_testApp::qt_testApp(QWidget *parent, Qt::WFlags flags)
    5. : QMainWindow(parent, flags), m_pTextWidget(0)
    6. {
    7. ui.setupUi(this);
    8. }
    9.  
    10. qt_testApp::~qt_testApp()
    11. {
    12.  
    13. }
    14.  
    15.  
    16. void qt_testApp::on_pushButton_clicked()
    17. {
    18. if(NULL == m_pTextWidget)
    19. {
    20. m_pTextWidget = new MyTextWidget;
    21. //m_pTextWidget->setBackgroundRole(QPalette::NoRole);
    22. //QPalette p = m_pTextWidget->palette();
    23. //p.setBrush(QPalette::Base, Qt::transparent);
    24. //m_pTextWidget->setPalette(p);
    25. }
    26.  
    27. m_pTextWidget->show();
    28. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. // mytextwidget.h created on Fri Nov 30 18:45:00 UTC 2007
    2. #ifndef MYTEXTWIDGET_H_AF06570D_B36E_4B82_8F97_C456AF4A38FD
    3. #define MYTEXTWIDGET_H_AF06570D_B36E_4B82_8F97_C456AF4A38FD
    4.  
    5. #include <QWidget>
    6. #include "ui_mytextwidget.h"
    7.  
    8. using namespace Ui;
    9.  
    10. class MyTextWidget : public QWidget, public MyTextWidgetClass
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. MyTextWidget(QWidget *parent = 0);
    16. ~MyTextWidget();
    17. private:
    18. virtual void paintEvent(QPaintEvent * event);
    19. };
    20.  
    21. #endif // MYTEXTWIDGET_H_AF06570D_B36E_4B82_8F97_C456AF4A38FD
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. // mytextwidget.cpp created on Fri Nov 30 18:45:00 UTC 2007
    2.  
    3. #include <QPainter>
    4. #include <QBitmap>
    5. #include "mytextwidget.h"
    6.  
    7. MyTextWidget::MyTextWidget(QWidget *parent)
    8. : QWidget(parent/*, Qt::FramelessWindowHint*/)
    9. {
    10. setupUi(this);
    11. }
    12.  
    13. MyTextWidget::~MyTextWidget()
    14. {
    15.  
    16. }
    17.  
    18. void MyTextWidget::paintEvent(QPaintEvent * event)
    19. {
    20. QWidget::paintEvent(event);
    21. QPixmap pm(size());
    22.  
    23. pm.fill(QColor(0,0,0,0));
    24.  
    25. QBitmap bm(pm);
    26.  
    27.  
    28. QPainter p(&pm);
    29. }
    To copy to clipboard, switch view to plain text mode 

    Thanks

    Vanir
    Last edited by wysota; 1st December 2007 at 03:30. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Transparent top-level window

    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.

  3. #3
    Join Date
    Nov 2007
    Posts
    12
    Thanks
    2
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Transparent top-level window

    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

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Transparent top-level window

    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.

  5. The following user says thank you to wysota for this useful post:

    Vanir (3rd December 2007)

Similar Threads

  1. Transparent window with QGLWidget
    By ultr in forum Qt Programming
    Replies: 4
    Last Post: 28th April 2008, 07:01
  2. Set a window as child at runtime
    By sabeesh in forum Qt Programming
    Replies: 1
    Last Post: 26th November 2007, 09:30
  3. Replies: 4
    Last Post: 4th June 2007, 12:07
  4. Regarding drawing on Transparent Window
    By Shalabh in forum Qt Programming
    Replies: 3
    Last Post: 31st May 2007, 10:32
  5. Making some part of window transparent
    By yogeshm02 in forum Qt Programming
    Replies: 1
    Last Post: 27th March 2006, 20:36

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.