Transparent QWebView with Qt5 - qt

We used to use trick https://www.qt.io/blog/2009/06/30/transparent-qwebview-or-qwebpage to make QWebView transparent with Qt4 as following, but same code give us blank background with Qt 5.2. Is there any way I can make that work with Qt5?
setAttribute(Qt::WA_TranslucentBackground, true);
setWindowFlags(Qt::FramelessWindowHint | Qt::Tool);
if(m_pWebView)
{
QPalette palette = m_pWebView->palette();
palette.setBrush(QPalette::Base, Qt::transparent);
m_pWebView->page()->setPalette(palette);
m_pWebView->setAttribute(Qt::WA_OpaquePaintEvent, false);
m_pWebView->setContextMenuPolicy(Qt::NoContextMenu);
}

The following two lines worked for me under QT5.4
setAttribute(Qt::WA_TranslucentBackground);
setStyleSheet("background:transparent");

Related

Setting window opacity on Qt linuxfb plugin does not work

I want to make a mask widget(top-level, means that window) with opacity 0.75 on linuxfb.
As it is a top-level widget(means that no parent).
I cannot use stylesheet "background-color:rgba(0,0,0,75%)".
I have tried:
MaskWidget::MaskWidget() {
setWindowFlags(Qt::FramelessWindowHint);
1.
setWindowState(Qt::WindowMaximized);
this->setWindowOpacity(0.75); //show warning "This plugin does not support setting window opacity"
2.
QRect desktopRect = QApplication::desktop()->availableGeometry(this);
setGeometry(0, 0, desktopRect.width(), desktopRect.height());
setAttribute(Qt::WA_TranslucentBackground); // it seems that not work
QGraphicsOpacityEffect* oe = new QGraphicsOpacityEffect();
oe->setOpacity(0.75);
this->setGraphicsEffect(oe);
}
The above methods work well in ubuntu and windows, but in my device with QPA linuxfb not work.

QComboBox background color

I have created a Qt HMI with QtDesigner and ui files. My QComboBox doesn't have the same background color in the designer and in real:
Designer:
Real life:
I am under Windows 7. Maybe it is OS dependent but I would like to have a white background.
I tried:
comboBox->setStyleSheet("QComboBox { background-color: white; }");
but it also paints the right arrow.
Any explanation?
Is the combo empty?
Try adding some elements and select one of them before running the "app".
Did you try changing QPalette::Base to white? You can do it without using any stylesheet.
QComboBox box = new QComboBox();
QPalette p = box.palette();
p.setColor(QPalette::Active, QPalette::Base, Qt::white);
p.setColor(QPalette::Inactive, QPalette::Base, Qt::white);
box.setPalette(p);
The QPalette::Base does not change the background of the QComboBox.
Instead I've used:
QPalette palette = ui->combo->palette();
palette.setColor(QPalette::Active, QPalette::Button, Qt::white);
palette.setColor(QPalette::Inactive, QPalette::Button, Qt::white);
ui->combo->setPalette(palette);
and it seems to work.

QPixmap and SVG

How would you suggest to handle svg with QPixmap?
The construct QPixmap(":/myfile.svg"); then call of scaled() does not work. The QPixmap gets pixelised.
Thx.
Now there is much simpler way without needing of SVG module
QIcon("filepath.svg").pixmap(QSize())
So simple and works fine. Atleast in my case it worked.
You should use SVGRenderer to render it onto a QImage. From there you can convert to a QPixmap with QPixmap::convertFromImage.
Something like that:
QSvgRenderer renderer(svg_file_name);
QPixmap pm(width, height);
pm.fill(fill_color);
QPainter painter(&pm);
renderer.render(&painter, pm.rect());
On Qt5.12 i managed to display SVG icons without pixellisation in a QLabel:
QPixmap logoPixmap(":my-logo.svg"); // set your logo here
auto logoLabel = new QLabel(this);
logoLabel->setPixmap(logoPixmap);
logoLabel->setScaledContents(true);
logoLabel->setFixedSize(176, 61); // set your size here

Change Horizontal Header Background Color of QCalendarWidget

I am currently using Eclipse 3.5.2 and Qt Jambi 4.7.2 in Ubuntu 11.04 Natty Narwhal
I have a class that currently extends QCalendarWidget.
I am trying to use style sheets to style my calendar widget. Right now, I am using QAbstractItemView to color the background but it only changes the background color of the cells with dates in them. The horizontal header piece that contains the days of the week names remains white no matter what I do. Is there a way to change the background color of this header using a style sheet?
Any help would be appreciated.
Thank you.
I've not tested it with versions below 4.8 but we had the same problem and the solution was quite simple. We used this CSS code:
QWidget#qt_calendar_navigationbar
{
background-color: #424242;
border: 1px solid #4f4f4f;
}
edit: Well, read before you post - I do not know if it works in your subclass, but it may be worth a try.
I assume that you are trying to use a .qss file and setting the stylesheet using that file.
The developers did not fully implement the use of external stylesheets with the QCalendarWidget so you will have to hack it a bit.
I would suggest that you added a constant to the .qss file that you are using something like:
#cons BACKGROUND_COLOR: cyan
Then you can read from the file in your code:
String color = "";
try {
URL qssFile = getClass().getResource("*PATHNAME*");
Scanner scanner = new Scanner(qssFile.openStream());
String nextLine;
try {
while (scanner.hasNextLine()){
nextLine = scanner.nextLine();
if (nextLine.contains("BACKGROUND_COLOR:")) {
color = nextLine.substring(nextLine.indexOf("BACKGROUND_COLOR:") + 17);
}
}
finally{
scanner.close();
}
} catch (IOException e) {
e.printStackTrace();
}
The 17 in the code refers to the length of BACKGROUND_COLOR: so we can get the text after it.
Next you will want to create a new QColor, setting it to the variable color from above. Next create a new QBrush from the QColor. Then create a new QTextCharFormat and set its background to be the QBrush. Finally, set the format by calling the setWeekdayTextFormat method on the QCalendarWidget, passing it the days of the week you wish to change and the format you want to change it to. The follow code sets the box for every day of the week in the HorizontalHeader to have a background color of cyan:
QColor c = new QColor(color);
QBrush b = new QBrush(c);
QTextCharFormat format = new QTextCharFormat();
format.setBackground(b);
this.setWeekdayTextFormat(Qt.DayOfWeek.Sunday, format);
this.setWeekdayTextFormat(Qt.DayOfWeek.Monday, format);
this.setWeekdayTextFormat(Qt.DayOfWeek.Tuesday, format);
this.setWeekdayTextFormat(Qt.DayOfWeek.Wednesday, format);
this.setWeekdayTextFormat(Qt.DayOfWeek.Thursday, format);
this.setWeekdayTextFormat(Qt.DayOfWeek.Friday, format);
this.setWeekdayTextFormat(Qt.DayOfWeek.Saturday, format);
Use all of that code together and you have yourself a way to change the HorizontalHeader background color by using .qss files (and more if you wish).

Display window full screen on secondary monitor using Qt

Seems to be possible with native controls (see here and here) so now I'm looking for some Qt code to do it.
I use this code for the second display in full screen successfully on both Windows & Linux
QRect screenres = QApplication::desktop()->screenGeometry(1/*screenNumber*/);
SecondDisplay secondDisplay = new SecondDisplay(); // Use your QWidget
secondDisplay->move(QPoint(screenres.x(), screenres.y()));
secondDisplay->resize(screenres.width(), screenres.height());
secondDisplay->showFullScreen();
One way of doing it in Qt5 is to use QWindow::setScreen to set the screen on which the window should be shown. QWidget has a windowHandle() that returns the pointer to the QWindow.
Here is how to show your widget on second screen in full-screen mode :
QWidget * widget = new QWidget();
widget->show();
widget->windowHandle()->setScreen(qApp->screens()[1]);
widget->showFullScreen();
My take on this:
auto const desktop(QApplication::desktop());
setGeometry(desktop->screenGeometry(1));
#ifndef Q_OS_WIN
setWindowState(Qt::WindowState(Qt::WindowFullScreen | windowState()));
#endif // Q_OS_WIN
showFullScreen first, then setGeometry.
Qt5 tested OK
This problem got solved while using window->showFullScreen() instead of window->show().

Resources