Archived:Transparent QDialog and QListWidget in Qt
(Created page with '__NOTOC__ __NOEDITSECTION__ {{CodeSnippet |id= |platform=S60 5th Edition |devices=Nokia N97 |category=Qt |subcategory= |creationdate=May 24, 2010 |keywords= QDialog, QListWidget …') |
|||
| Line 110: | Line 110: | ||
QDialog has transparent background. | QDialog has transparent background. | ||
| + | |||
| + | ==See also== | ||
| + | [[http://doc.trolltech.com/4.6/stylesheet-reference.html]] | ||
[[Category:Qt]][[Category:Code Examples]][[Category:Code Snippet]] | [[Category:Qt]][[Category:Code Examples]][[Category:Code Snippet]] | ||
Revision as of 14:53, 24 May 2010
Article Metadata
Tested with
Devices(s): Nokia N97
Compatibility
Platform(s): S60 5th Edition
Article
Keywords: QDialog, QListWidget
Created: (24 May 2010)
Last edited: tepaa
(24 May 2010)
Overview
This code snippet demonstrates how to create transparent QDialog that has QPixmap picture as background and has transparent QListWidget on center of the screen.
Header
#include <QDialog>
#include <QListWidget>
class TransparentDlg : public QDialog
{
public:
TransparentDlg(QWidget *parent = 0);
~TransparentDlg();
void resizeEvent(QResizeEvent *);
void paintEvent(QPaintEvent *);
private:
QPixmap m_pixmapBackground;
QListWidget* m_listWidget;
};
Source
Transparent QDialog that shows picture as background.
#include <QPainter>
TransparentDlg::TransparentDlg(QWidget *parent)
: QDialog(parent)
{
// Set QDialog background as transparent
setAttribute(Qt::WA_NoSystemBackground);
// Load picture for dialog background
m_pixmapBackground.load(":/background.png");
// Create QListWidget
m_listWidget = new QListWidget(this);
new QListWidgetItem("Row One",m_listWidget);
new QListWidgetItem("Row Two",m_listWidget);
new QListWidgetItem("Row Three",m_listWidget);
new QListWidgetItem("Row Four",m_listWidget);
}
TransparentDlg::~TransparentDlg()
{
}
void TransparentDlg::resizeEvent(QResizeEvent *event)
{
// Set size for the background picture
QSize backSize = size();
backSize -= QSize(size().width()/4,size().height()/4);
m_pixmapBackground = m_pixmapBackground.scaled(backSize);
// Set size for the QListWidget
QSize listSize = backSize - QSize(40,40);
QPoint p = QPoint((size().width()-listSize.width())/2,
(size().height()-listSize.height())/2);
m_listWidget->setGeometry(QRect(p,listSize));
}
void TransparentDlg::paintEvent(QPaintEvent *event)
{
QDialog::paintEvent(event);
QPainter p(this);
p.drawPixmap((size().width()-m_pixmapBackground.size().width())/2,
(size().height()-m_pixmapBackground.size().height())/2,m_pixmapBackground);
}
For getting QListWidget to look as transparent, define new style and set stylesheet for it using QApplication::setStyleSheet()
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
// Set style for the QListWidget and QListWidgetItem
QString style;
style += "QListWidget {background-color: transparent;}";
style += "QListWidget::item {background-color: transparent;}";
style += "QListWidget::item {selection-color: white;}";
style += "QListWidget::item {color: black;}";
a.setStyleSheet(style);
qtSnippets w;
w.showMaximized();
return a.exec();
}
Postconditions
QDialog has transparent background.
See also
[[1]]

