Transparent Background in QWebEnginePage - c++

We are trying to port some application from Qt 4 to Qt 5.4. The Qt 5.4 has a new web engine. We used to make the background of QWebView and QWebPage to be transparent:
view = new QWebView(this);
QPalette palette = view->palette();
palette.setBrush(QPalette::Base, Qt::transparent);
view->page()->setPalette(palette);
view->setAttribute(Qt::WA_OpaquePaintEvent, false);
But this code doesn't work for QWebEngineView and QWebEnginePage. The point is that QWebEnginePage doesn't have such an API like setPalette.
Can anyone find a way to solve this?

As mentioned in https://bugreports.qt.io/browse/QTBUG-41960, this is now working by simply using this line:
webEngineView->page()->setBackgroundColor(Qt::transparent);
I've tried it in Qt 5.6 and it works well.
Update: In order to make this answer more helpful, let me show all relevant code.
In MainWindow, I have set this:
setAttribute(Qt::WA_TranslucentBackground);
setAutoFillBackground(true);
setWindowFlags(Qt::FramelessWindowHint);
For the webEngineView object, I have set these attributes:
webEngineView->setAttribute(Qt::WA_TranslucentBackground);
webEngineView->setStyleSheet("background:transparent");
webEnginePage = webEngineView->page();
// https://bugreports.qt.io/browse/QTBUG-41960
webEnginePage->setBackgroundColor(Qt::transparent);

No. A partial solution has been committed to upstream, but it covers only QtQuick and you can't have any elements on top:
https://bugreports.qt.io/browse/QTBUG-41960

Related

Mapgraphics library on github. C++/Qt/OpenStreetMap

I have a question regarding this library: https://github.com/raptorswing/MapGraphics. Contact the maker did not work, so I hope for help here, can someone faced with this same problem.
How do I add an object to my map in this library? Tried to do here is so:
MapGraphicsScene *scene = new MapGraphicsScene();
MapGraphicsView *view = new MapGraphicsView(scene);
LineObject line(Position(92.860984, 56.006355), Position(92, 56), 10);
QPainter linePainter;
QColor red(255, 0, 0);
linePainter.setPen(red);
QStyleOptionGraphicsItem itm;
line.paint(&linePainter, &itm, view);
QSharedPointer<OSMTileSource> osmTiles(new OSMTileSource(OSMTileSource::OSMTiles), &QObject::deleteLater);
QSharedPointer<CompositeTileSource> composite(new CompositeTileSource(), &QObject::deleteLater);
composite->addSourceBottom(osmTiles);
view->setTileSource(composite);
But it gave no results. Can you help me? :(
Unfortunately the relevant bit of code isn't in your snippet, but I wonder if it's http vs https access? When you try and download a tile such as this http one it'll now redirect to https. This caught out a number of clients (to my knowledge Java-based and Flash-based ones).
The github project says "It is a Qt map widget that can use tiles from MapQuest, Openstreetmap, or a custom source you define" which suggests it's actually quite old, since Mapquest haven't had their own map tiles for a very long time. Try using a custom source and define https access to OSM tiles.
It is enough to do the following:
MapGraphicsObject *line = new LineObject(Position(92.860984, 56.006355), Position(92, 56), 10);
view->scene().addObject(line);
This should be done with any type of object.

Set alignment of QGroupBox title

I try programmatically set alignment of title of QGroupBox. According to the documentation I try to do it in the following way:
MainWidget::MainWidget (QWidget * parent)
: QWidget (parent)
{
setWindowTitle (tr ("QGropBox Title Alignment issue") );
QGroupBox * group = new QGroupBox ("Group Title", this);
QVBoxLayout * layoutTop = new QVBoxLayout ();
layoutTop->addWidget (group);
group->setAlignment (Qt::AlignHCenter);
this->setLayout (layoutTop);
}
But I have got unexpected result and title aligned with the left-hand side of the group box.
Such behaviour I have got with the environment:
Debian 9.0 (sid);
Qt 5.5.1 (installed from repository);
XMonad DE.
The same behaviour I have got with the next environment:
Ubuntu 15.10;
Qt 5.4 (installed manually using offline-installer);
KDE.
But! If I use Qt installed from repository (Qt 5.4.2) the title will aligned with the centre by default. But if I change align to the left it will not changed.
I try find some solution with Google, but I haven't found similar questions and this is bad sign and usually this is means that I do some thing wrong.
So my question is: Why I can't manage by alignment of the group box title.
It seems like in Qt 5.5.1 there is a bug for some visual styles when QGroupBox title alignment is not taken into account when title's rect is computed: QTBUG-49068: QGroupBox title does not follow alignment with fusion style.
You can download most recent version from git repository, build it and check if the bug has been fixed (is has been for Fusion style: qt commit 139953).
If bug still occurs, I think you should submit an issue to Qt bugtracker.

How to put osgEarth's ViewerWidget into an tabbed MdiArea?

Is there something special about putting osgEarth's ViewerViewer into a QMdiArea? I created a QMdiArea as central Widget (called setCentralWidget) instead of taking osgEarth's viewer directly as central widget.
QMdiArea *mdiArea = new QMdiArea(this);
setCentralWidget(mdiArea); // call to QMainWindows method, snippet is taken from app's MainWindow
mdiArea->addSubWindow(viewerWidget); // this doesn't work, globe is not drawn
Everything I tried didn't worked... except osgEarth's ViewerWidget is set as central widget of my MainWindow. Also tried MultiViewerWidget without any success but because I need only one view the ViewerWidget should be ok, or not?
I had a look into the examples but didn't succed to use one of them as starting point.
Any hints? Thank's in advance.
you can try this, where Form1 is a QDialog
in main.cpp
int main()
{
QApplication a(argc, argv);
Form1 w=new Form1();//qdialog
.................//do something to initial the map
w.loadWidget(viewerWidget);
w.show();//the order of the loadwiget() and show() is important!!!!!
a.exec();
}
in Form1.cpp
void Form1::loadWidget(QWidget *qwidget)
{
qwidget->setMinimumSize( ui.mdiArea->width(),ui.mdiArea->height());
QMdiSubWindow * subW=ui.mdiArea->addSubWindow(qwidget);
subW->setWindowFlags(Qt::SubWindow | Qt::FramelessWindowHint);
subW->maximumSize();
}
This works well with qt 4.8.4+osgearth 2.3
Try setting the subwindow's geometry before starting the UI.
QMdiSubWindow* sw = mdiArea->addSubWindow(viewerWidget);
sw->setGeometry(...);
Otherwise OSG will probably become confused.
Got this answer by Gwaldron in the osgEarth forum here and it worked.
Also setting a minimum size for the viewerWidget will help (e.g. for positioning on TabWidget). See my question and answer here.

Connecting qml-signals to Qt

I'm trying to use a qml-grid view in my code. I'm trying to couple it with my C++ code.
I've dynamically created a list view model and passed across the qml file. It works fine.
However, I'm facing trouble when I want to connect a Qml signal to Qt/c++ code. I've handled mouseArea in my Qml-rectangle and emitting a signal from there.
I'm trying to connect to the signal as follows:
QDeclarativeView *pQMLContainer = NULL;
TempWidget *pTemp = new TempWidget();
pQMLContainer = new QDeclarativeView(pTemp);
pQMLContainer->setResizeMode(QDeclarativeView::SizeRootObjectToView);
pQMLContainer->rootContext()->setContextProperty("imgModel", createModel() );
pQMLContainer->setSource(QUrl("../Temp/qml/gridview-example.qml"));
QObject *rootObject = dynamic_cast<QObject*>pQMLContainer->rootObject();
QObject::connect(rootObject, SIGNAL(keyPressed()), pTemp, SLOT(onKeyPressed()));
When the connect statement runs, I get an error: cannot connect to "null" object.
On debugging, I found I could never get "rootObject" as a valid pointer.
Where am I going wrong?
Thanks
Can you try this ? (it is example code from Qt Docs)
QObject *item = pQMLContainer->rootObject();
QObject::connect(item, SIGNAL(keyPressed()),
pTemp, SLOT(onKeyPressed()));
The code is pretty much straight:
in .cpp file:
ui->declarativeView->setSource(QUrl("qrc:/Resources/main.qml"));
QGraphicsObject *obj = ui->declarativeView->rootObject();
connect ( obj, SIGNAL(clicked()), this, SLOT(itemClicked()));
and QML File:
import Qt 4.7
Rectangle {
width: 100
height: 100
id: rect
signal clicked
Text {
text: "Hello World"
anchors.centerIn: parent
}
MouseArea {
anchors.fill: parent
onClicked: {
rect.clicked();
}
}
}
one more thing, check the location of your qml file, it should be accessible to the binary.
Perhaps you should use qobject_cast instead of dynamic_cast? See e.g. question
dynamic_cast returns NULL but it shouldn't
QGraphicsObject is a QObject so no cast should be required. If your compiler complains, try adding #include <QGraphicsObject>.
Just casting without the compiler knowing the classes is asking for trouble. (Especially as there is multiple inheritance involved.)
I could finally get this working. I'm not sure if this is the real solution to the problem, but finally this got it working:
I was setting the qml path as a relative path to my working folder. And yes the path was indeed correct, as I could see the qml and its contents. I just happened to change the qml path from relative to the working folder to relative to "qrc" as:
pQMLContainer->setSource(QUrl("qrc:/gridview-example.qml"));
instead of:
pQMLContainer->setSource(QUrl("../Temp/qml/gridview-example.qml"));
and it started working. I'm not sure if I had to add the qml to the qrc (I've just started using qml).
Thanks everyone for your support!
Mots

setCentralWidget() causing the QMainWindow to crash.. Why?

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
this->setupUi(this);
this->setupActions();
this->setWindowTitle(tr("CuteEdit"));
label = new QLabel(tr("No Open Files"));
this->setCentralWidget(label);
label->setAlignment(Qt::AlignCenter);
}
By above code, I get a GUI like this(Its a screenshot of whole screen, Only observe the window displayed in middle of page of ebook). (I used QT Designer)
Now, i want user to select File->Open.. A Dialog appears and file gets selected.. Its contents are to be displayed in *textEdit widget..
Function for that is below..
void MainWindow::loadFile()
{
QString filename = QFileDialog::getOpenFileName(this);
QFile file(filename);
if (file.open(QIODevice::ReadOnly|QIODevice::Text))
{
label->hide();
textEdit->setPlainText(file.readAll());
mFilePath = filename;
QMainWindow::statusBar()->showMessage(tr("File successfully loaded."), 3000);
}
}
The window crashes at line:-
textEdit->setPlainText(file.readAll());
But if i comment the line:-
this->setCentralWidget(label);
i mean i remove label as being the central widget, the program runs as expected.. Why?
And also, I am not clear about the concept of CentralWidget. Pls guide.
JimDaniel is right in his last edit. Take a look at the source code of setCentralWidget():
void QMainWindow::setCentralWidget(QWidget *widget)
{
Q_D(QMainWindow);
if (d->layout->centralWidget() && d->layout->centralWidget() != widget) {
d->layout->centralWidget()->hide();
d->layout->centralWidget()->deleteLater();
}
d->layout->setCentralWidget(widget);
}
Do you see that if your MainWindow already had centralWidget() Qt schedules this object for deletion by deleteLater()?
And centralWidget() is the root widget for all layouts and other widgets in QMainWindow. Not the widget which is centered on window. So each QMainWindow produced by master in Qt Creator already has this root widget. (Take a look at your ui_mainwindow.h as JimDaniel proposed and you will see).
And you schedule this root widget for deletion in your window constructor! Nonsense! =)
I think for you it's a good idea to start new year by reading some book on Qt. =)
Happy New Year!
Are you sure it's not label->hide() that's crashing the app? Perhaps Qt doesn't like you hiding the central widget. I use Qt but I don't mess with QMainWindow that often.
EDIT: I compiled your code. I can help you a bit. Not sure what the ultimate reason is as I don't use the form generator, but you probably shouldn't be resetting the central widget to your label, as it's also set by the designer, if you open the ui_mainwindow.h file and look in setupGui() you can see that it has a widget called centralWidget that's already set. Since you have used the designer for your GUI, I would use it all the way and put the label widget in there as well. That will likely fix your problems. Maybe someone else can be of more help...
I'm not sure I understood your problem, neither what the guys above said (which I guess are valid information) and it seems to be an old topic.
However, I think I had a problem that looks like this and solved it, so I want to contribute my solution in case it helps anyone.
I was trying to "reset" central widget using QLabels. I had three different ones, switch from first to second, then to third and failed to switch back to the first one.
This is my solution that worked:
Header file
QLabel *imageLabel;
Constructor
imageLabel = new QLabel("<img src='/folder/etc.jpg' />");
this->setCentralWidget(imageLabel);
Reset
imageLabel = NULL;
imageLabel = new QLabel("<img src='/folder/etc.jpg' />");
this->setCentralWidget(imageLabel);
Hope that helps
Aris