Border changes width depending on rectangle width - qt

I have a rounded rectangle in QML code and I noticed strange behavior of rectangle's border, when I resize the rectangle its border appears to be flickering a little and if I look closely I can see that its border seems to be changing its width from 1 to 2 and back or something like that, while in code it definitely set to 1 and doesn't depend on anything else. Also rectangle corners seem to be a little bit wider than border at straight sections. I tried to take screenshots of such rectangles but on the screenshots rectangles looks alright so I suppose it may be some rendering issue? It definitely noticeable in the app. On screenshots you can see that the second rectangle has its borders a little bit blurry, thats when the border appears to be wider than normal.
Is this a known problem and are there any settings in Qt I need to change to get consistent border width on my elements?
I'm using Qt 5.15, QML, C++. I could reproduce this behavior on Windows 10 on two different 1440p monitors.
Edit:
Since my main project is pretty big I will provide example project where I was able to reproduce the issue:
main.cpp
#include <QGuiApplication>
#include <QSurfaceFormat>
#include <QQmlApplicationEngine>
int main(int argc, char *argv[])
{
#if defined(Q_OS_WIN)
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
return app.exec();
}
main.qml:
import QtQuick 2.9
import QtQuick.Window 2.2
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
Rectangle
{
anchors.horizontalCenter: parent.horizontalCenter
anchors.verticalCenter: parent.verticalCenter
width: parent.width / 3
height: parent.height / 3
color: "#F8F8F8"
radius: 8
border
{
color: "red"
width: 1
}
Text
{
id: valueField
anchors.horizontalCenter: parent.horizontalCenter
anchors.verticalCenter: parent.verticalCenter
font.pointSize: 10
text: "100"
color: "black"
}
}
}
The problem is reproducable when width and height of rectangle are bound to width and height of window, if I set them to one specific value - no such issue is present.

I found that if I do height: Math.round(parent.height / 3) instead of simple height: parent.height / 3, then the issue is gone. As per the comment from #DFaller :
I believe you are correct that this is the solution - QML accepts floats for size but seems to render only in integers, which can causes rounding issues where borders show as either 0 or 2 pixels instead of 1. Rounding as you did has fixed this issue for me in the past.
So posting this as the answer for now.

Related

QtWebEngine displays partially without margins

WebEngineView should be displayed in the whole window, but it's not. Right margin strip and bottom margin strip is not displayed. Instead a strip of (red) background is visible. Yet I can click on red background on the input search formular and it can be filled. There shouldnt be any background visible.
I also tried QWebEngineView instead of QQmlApplicationEngine and it is exactly the same.
This code is run by Qt 6.4.0.
In Qt 5.12.5 it is displayed correctly.
What to do to show WebEngineView in the whole window ?
main.cpp:
int main(int argc, char *argv[])
{
QtWebEngineQuick::initialize();
QApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/webengine.qml")));
return app.exec();
}
webengine.qml:
import QtQuick
import QtQuick.Window
import QtWebEngine
Window {
width: 800
height: 600
visible: true
color: "red"
WebEngineView {
id: webEngineView
anchors.fill: parent
url: "https://doc.qt.io/qt-6/qtwebengine-webenginequick-minimal-example.html"
}
}
The gui:
This should already be fixed in 6.4.0, was this observed during the beta, if so, then try with the final release. If observed with the final, please open a bug report for at Qt.

Huge flickering while main window height is animated

In a Qt Quick application, I want to animate the main window height when I click on a toggle button, in order to show or hide a kind of tray panel. The main form content contains a header frame, a swipe view and a grid view below it.
To reach the desired effect, I added the following animations in my qss code, which are run depending of my toggle button state:
ParallelAnimation
{
id: one_dev_connected_toggle_window_height_increase
running: false
NumberAnimation { target: mainWindow; property: "height"; to: 750; easing.type: Easing.InOutQuad; duration: 500}
}
ParallelAnimation
{
id: one_dev_connected_toggle_window_height_decrease
running: false
NumberAnimation { target: mainWindow; property: "height"; to: 450; easing.type: Easing.InOutQuad; duration: 500}
}
When I try to open the tray, the animation cause a huge flickering on my whole interface. However, when I close the tray, the animation cause no flickering at all, and the effect is smooth, as I expected.
My main window is declared as follow:
ApplicationWindow
{
id: mainWindow
visible: true
width: 700
height: 750
color: "#000000"
title: qsTr("Drag&Drop App")
flags: Qt.Window | Qt.FramelessWindowHint
....
Can someone explain me why I'm facing a such flickering? What should I change to fix it?
First I tried to reproduce your behavior with a small app, which is listed below:
main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
int main(int argc, char* argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
QQmlContext* context = engine.rootContext();
// Adding the following line helps to remove the flickering
app.setAttribute(Qt::ApplicationAttribute::AA_ShareOpenGLContexts, true);
engine.load(QUrl("./data/main.qml"));
return app.exec();
}
main.qml
import QtQuick 2.13
import QtQuick.Controls 2.5
ApplicationWindow {
id: mainWindow
width: 800; height: 1000
title: "Animation Flickers"
visible: true
property bool large:true
MouseArea {
anchors.fill: parent
onClicked: {
if (mainWindow.large) {
decr.start()
} else {
incr.start();
}
mainWindow.large=!mainWindow.large;
}
}
ParallelAnimation
{
id: incr
running: false
NumberAnimation { target: mainWindow; property: "height"; to: 750; easing.type: Easing.InOutQuad; duration: 500}
}
ParallelAnimation
{
id: decr
running: false
NumberAnimation { target: mainWindow; property: "height"; to: 450; easing.type: Easing.InOutQuad; duration: 500}
}
}
Then I activated the application attribute AA_ShareOpenGLContexts and the flickering vanished. There might be many reasons for flickering and this might just remove one reason. I already experienced flickering by a non fitting graphics card driver. You should also consider to run your program on a different machine.
Please report back, if my solution didn't solved your problem.
Playing with the parameters, I finally found an acceptable solution.
Here's what I noticed:
Following the Aleph0's reply, I played around the OpenGL flags. I noticed that using the Qt::ApplicationAttribute::AA_UseOpenGLES flag resolved completely the flickering, however it caused a strange side effect in my case: the height of all components contained in my window became inconsistent while the main form height changed, causing a kind of wobbling during the animation, even with the correct anchoring and min and max height constraints defined.
On my main interface I removed the anchoring and used layouts instead. This resolved the strange wobbling during the height change, and almost all the flicker issues. I tested on different computers with different OS, the flickering appear only rarely in several particular cases.
However the wobbling still appears while the layouts are used if OpenGLES is used.
So replacing anchors by layouts was the solution in my case, even if it works not perfectly. But at least the result is acceptable.

Display bug when moving a transparent window QtQuick

So for my current project I need to use a transparent window.
On the creation of the window no problem it works is when you move the window that there is a problem.
The transparency does not apply because it considers that the window is part of the background and reproduces it in the new rendering.
I do not know if I'm very clear I put a picture in link it's more speaking sorry for the quality I had to take it with the laptop because when we capture it "refresh" the display and transparency becomes normal again.
Another surprising thing is that the transparency does not bug if what is behind is chrome ...
So basically the question is how to have a "normal" behavior?
Here I hope that you can help me I see no solution there.
At the code level:
main.cpp :
#include <QQuickView>
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QQuickView * view = new QQuickView;
view->setTitle("Test");
view->setSource(QUrl::fromLocalFile ("main.qml"));
view->setColor(Qt::transparent);
view->show();
return a.exec();
}
main.qml :
import QtQuick 2.0
Rectangle {
width: 400
height: 400
color : "#00000000"
Rectangle {
anchors.centerIn: parent
color: "red"
width: 100
height: 100
}
}
Result

How to recieve Qt signal entered() in MouseArea from Touch?

I use MouseArea for buttons in my QT app and need to know, when the user moves his finger on the touch on a button. On W7 using a mouse, I get this information with onEntered from MouseArea.
The problem: on the target platform with touchscreen, the entered-signal is only fired, when I press the MouseArea (first touch inside MouseArea), then move my finger out (exited-signal), then move the finger back in (entered-signal). But when the first touch is outside the area and I enter it, entered-signal is not fired.
I also tried MultiPointTouchArea with onTouchUpdated and onUpdated, same behaviour.
I am using Qt 5.5.1 on embedded linux.
Is there something I can do to make the MouseArea always fire the entered-signal? Or should I use something other than MouseArea or MultiPointTouchArea?
As a workaround I could think of a MouseArea on the complete window, with information where all the buttons are and checking on each positionChanged, if a button was entered, but I'm sure there must be a better solution.
Edit: When using a mouse on target platform, the entered-signal is fired when moving the mouse into the area.
This is my (simplified) code:
main.cpp
#include <QApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QtQml>
#include "uart_terminal.h"
int main(int argc, char *argv[])
{
system("mount /dev/mmcblk0p1 /mnt/sdcard/");
setenv("QT_QPA_EGLFS_PHYSICAL_WIDTH", "376", 1);
setenv("QT_QPA_EGLFS_PHYSICAL_HEIGHT", "301", 1);
QApplication app(argc, argv);
qmlRegisterType<UART_terminal>("com.uart", 1, 0, "UARTTerminal");
QQmlApplicationEngine engine;
engine.rootContext()->setContextProperty("applicationDirPath", QGuiApplication::applicationDirPath());
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
return app.exec();
}
main.qml:
Window
{
id: rootWindow
visible: true
width: 1280
height: 1024
Rectangle
{
id: test
color: "red"
width: 300
height: 300
MouseArea
{
anchors.fill: parent
hoverEnabled: true
acceptedButtons: Qt.AllButtons
onEntered:
{
console.log("entered")
test.color = "blue"
}
onExited: test.color = "red"
onCanceled: console.log("canceled")
onPositionChanged:
{
console.log("pos changed, containsMouse", containsMouse)
}
}
/*MultiPointTouchArea
{
anchors.fill: parent
onTouchUpdated: console.log("touch updated")
onUpdated: console.log("touchlist updated")
}*/
}
}
So as I didn't find any other solution, I had to implement the mentioned work around. I put a MouseArea "buttonContainer" on the whole screen, behind all other MouseAreas. Note that I have to propagate the positionChanged signal from each MouseArea, otherwise it won't create my button-entered signal when I started the touch in one button and then enter another one.
onPositionChanged:
{
// propagate event to buttonContainer with absolute mouse position
var position = roundMouseArea.mapToItem(root)
buttonContainer.xOffsetMouse = position.x
buttonContainer.yOffsetMouse = position.y
buttonContainer.positionChanged(mouse)
}
Then in buttonContainer's onPositionChanged I have to check for all Buttons that are currenty visible if they were entered. Don't forget to map each buttons position again with btn.mapToItem(buttonContainer) and to reset x- and yOffsetMouse in buttonContainer's onPressed.
In my case I had to do much more stuff like
check if button is disabled anyways, although visible
remember enteredBtn
set bool for each button if it's already entered from my container, so I don't get entered-signal at each positionChanged while I stay in that button
also check for button exited
set btnPressed true or false accordingly, eg. also at buttonContainer's onReleased
reset enteredBtn to undefined accordingly and check for that, before referencing any method or property of enteredBtn (eg. at buttonContainer's onReleased)
I think that was all. Don't want to copy my whole code here, just mention some aspects you might also need to consider if you have the same issue.

Why does a TextArea with NoWrap always cause an "anchor loop detected" warning?

Why is a TextArea with
wrapMode: TextEdit.NoWrap
always causing
file:///C:/Qt/5.5/mingw492_32/qml/QtQuick/Controls/ScrollView.qml:340:13: QML Item: Possible anchor loop detected on fill.
when I run it?
I am running Qt 5.5 on a 64-bit Windows 7 machine, and compiling with MinGW.
Here is my QML code test.qml:
import QtQuick 2.4
import QtQuick.Controls 1.3
ApplicationWindow {
title: "test window"
width: 500
height: 500
visible: true
TextArea {
wrapMode: TextEdit.NoWrap
}
}
Here is my C++ code main.c:
#include <QApplication>
#include <QQmlApplicationEngine>
#include <QtQml>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/test.qml")));
return app.exec();
}
Even if I add anchors.fill: parent to the TextArea, I still get the warnings.
As a second part of this question, is this warning something I should be worried about, or is it something I can safely ignore?
I think it's a bug from Qt, you can ignore it.
When created, TextArea have a width != 0, even if it's empty. When you enter a text that have an implicitWidth smaller then the (default) width of the TextArea, you will get this warning.
A workaround is to assign the wrapMode property in the Component.onCompleted handler:
Component.onCompleted: wrapMode = TextEdit.NoWrap

Resources