I am trying to get my container with a qquickview in it to be transparent, and only display the elements from the QML file without the background.
Is there a nicer way to implement this? Here is the relevant code, you can see I have commented out adding the QML to it so that the only offender is the QWidget containing a QQuickView
The QML will make a menu similar to Apple's "Cover Flow" using only text. I only want the text it generates to be visible.
Currently it has a solid white background.
QQuickView *view = new QQuickView();
QWidget *container = QWidget::createWindowContainer(view, this);
container->setObjectName("wrappingContainer");
container->setMinimumSize(1000, 240);
container->setMaximumSize(1000, 240);
container->setFocusPolicy(Qt::TabFocus);
//view->setSource(QUrl("qrc:/qml/wrappingMenu.qml"));
ui->testLayout->addWidget(container);
Try QQuickWidget:
#include <QtWidgets>
#include <QQuickWidget>
class Widget : public QWidget
{
Q_OBJECT
public:
Widget() {
setStyleSheet("background-color: 'grey';");
mQQuickWidget = new QQuickWidget(QUrl(QStringLiteral("main.qml")), this);
mQQuickWidget->setResizeMode(QQuickWidget::SizeRootObjectToView);
mQQuickWidget->setAttribute(Qt::WA_AlwaysStackOnTop);
mQQuickWidget->setClearColor(Qt::transparent);
mQQuickWidget->resize(400, 400);
mQQuickWidget->raise();
}
private:
QQuickWidget *mQQuickWidget;
};
int main(int argc, char** argv)
{
QApplication app(argc, argv);
Widget widget;
widget.resize(400, 400);
widget.show();
return app.exec();
}
#include "main.moc"
main.qml:
import QtQuick 2.2
Item {
Text {
text: "Qt Quick Text"
font.pixelSize: 32
anchors.centerIn: parent
}
}
I doubt it could be done for a QQuickView embedded in a QWidget. But you can have a transparent QQuickView like :
QQuickView view;
view.setSurfaceType(QSurface::OpenGLSurface);
QSurfaceFormat format;
format.setAlphaBufferSize(8);
format.setRenderableType(QSurfaceFormat::OpenGL);
view.setFormat(format);
view.setColor(QColor(Qt::transparent));
view.setClearBeforeRendering(true);
view.setFlags(Qt::FramelessWindowHint);
view.setSource(QStringLiteral("qrc:/qml/wrappingMenu.qml"));
view.show();
You can do it with:
QQuickView view;
view.setColor(Qt::transparent);
view.setSource("main.qml");
Related
I was trying to change the background color of a QPlainTextEdit widget to black and apply a glow effect to it. But when I apply a DropShadowEffect to a QTextEdit or a QPlainTextEdit widget, its background color reverts back to the original and refuses to change. I'm using Qt version 5.12.2. Here's the code:
#include <QtWidgets>
int main(int argc, char** argv)
{
QApplication app(argc, argv);
QMainWindow window;
window.setWindowTitle("Title");
window.setStyleSheet("QMainWindow{background-color: #191b21}");
window.setFixedSize(800, 600);
window.show();
QPlainTextEdit* w = new QPlainTextEdit(&window);
w->setGeometry(250, 50, 300, 50);
w->setStyleSheet("QPlainTextEdit{background-color: black; color: white;}");
QGraphicsDropShadowEffect* effect = new QGraphicsDropShadowEffect();
effect->setColor("#59f1ff");
effect->setBlurRadius(30);
effect->setOffset(.0);
w->setGraphicsEffect(effect);
w->show();
return app.exec();
}
I've tried using QPalette but it won't work too. What am I doing wrong?
I'm trying to display a Q3DScatter graph in a Main Window designed with Qt Designer so I've added a widget to the main window but I don't know how to embed the graph object in this widget. I tried to create a widget "container" programmatically and embed the graph in it and then putting the widget in a QHBoxLayout (which has been added to the main window using Qt Designer) using the following code in my main window's constructor:
...
Q3DScatter *graph = new Q3DScatter();
QWidget *container = QWidget::createWindowContainer(graph);
ui->horizontal_layout->addWidget(container, 1);
But the window appears to be empty when executing. I'd really appreciate some help.
EDIT: Here's the full code of my main window constructor:
ResultsWindow::ResultsWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::ResultsWindow)
{
ui->setupUi(this);
Q3DScatter *graph = new Q3DScatter();
QWidget *container = QWidget::createWindowContainer(graph);
ui->horizontal_layout->addWidget(container, 1);
}
And here's the main code:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
ResultsWindow w;
w.show();
return a.exec();
}
EDIT 2: I forgot to specify that the horizontal layout is embedded in a GridLayout. I tried to create a new project and my code actually works perfectly when I add a horizontal layout directly to the main window. So could the problem be due to the GridLayout?
There is following code. It creates QGraphicsView object, sets a scene and then there is created a QGraphicsWebView object which is added to the scene:
QGraphicsWebView* graphicsWebView;
QGraphicsScene* graphicsScene;
QGraphicsView* graphicsView;
QMainWindow* mainWindow;
class Deleter : public QObject
{
Q_OBJECT
public slots:
void deleteWebView()
{
mainWindow->hide();
mainWindow->centralWidget()->setParent(0);
mainWindow->setCentralWidget(new QWidget());
delete graphicsView; // <-- crashes about 2 seconds after that
}
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
mainWindow = new QMainWindow;
graphicsView = new QGraphicsView;
graphicsScene = new QGraphicsScene(graphicsView);
graphicsView->setScene(graphicsScene);
graphicsWebView = new QGraphicsWebView;
graphicsWebView->setUrl(QUrl("http://www.google.com"));
graphicsView->scene()->addItem(graphicsWebView);
graphicsView->setViewport(new QGLWidget());
graphicsView->setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
mainWindow->setAttribute(Qt::WA_TranslucentBackground);
mainWindow->setCentralWidget(graphicsView);
mainWindow->show();
Deleter d;
QTimer::singleShot(10000, &d, SLOT(deleteWebView()));
return app.exec();
}
#include "main.moc"
10 seconds later there is invoked a slot which tries to delete QGraphicsView object. The problem is that when I try to delete graphicsView, the program crashes after about 2 seconds. The backtraces are garbage. Theoretically QGraphicsView object should remove its children and the child is QGraphicsScene object. The scene should remove its child which is QGraphicsWebView object.
How to correctly delete QGraphicsView object without crashing the process?
This is Qt 4.8
I don't understand why it doesn't clean up properly, but I would not make graphicsView a parent of your scene. Instead of giving the scene a parent, just delete it in your deleteWebView slot after the view.
I can't reproduce. The following works consistently and without crashes on Qt 4.8.7 on OS X:
#include <QtGui>
#include <QGraphicsWebView>
#include <QGLWidget>
class Window : public QMainWindow {
Q_OBJECT
QWidget central;
QVBoxLayout layout{¢ral};
QPointer<QGraphicsView> view;
QPushButton button{"Toggle View"};
Q_SLOT void toggle() {
if (!view) {
view = new QGraphicsView;
auto scene = new QGraphicsScene(view);
auto webView = new QGraphicsWebView;
webView->setUrl(QUrl("http://www.google.com"));
scene->addItem(webView);
view->setScene(scene);
view->setViewport(new QGLWidget);
view->setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
layout.addWidget(view);
} else {
delete view;
view = nullptr;
}
}
public:
Window() {
layout.addWidget(&button);
layout.addStretch(1);
setAttribute(Qt::WA_TranslucentBackground);
setCentralWidget(¢ral);
connect(&button, SIGNAL(clicked(bool)), SLOT(toggle()));
}
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Window w;
w.show();
return app.exec();
}
#include "main.moc"
I have this problem for months. I try to implement a webview on iOS with QtWebView by Qml and embed the webview into a widget by QWidget::createWindowContainer (following a online tutorial). But I just can't get this widget to fill the parent widget, nor change its size at all.
I have write the following minimal example for explaining the problem:
main.cpp
#include <QApplication>
#include <QQuickView>
#include <QBoxLayout>
#include <QWidget>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget *mainwindow = new QWidget();
mainwindow->setStyleSheet("background-color:red;"); // so that we can see the problem more clearly
QHBoxLayout *layout = new QHBoxLayout(mainwindow);
QQuickView *view = new QQuickView();
QWidget* container = QWidget::createWindowContainer(view, mainwindow);
// container->setMinimumSize(200, 200);
// container->setMaximumSize(200, 200); // the displaying size doesn't change with or without these lines
// container->setFocusPolicy(Qt::TabFocus);
view->setSource(QUrl("qrc:///webview.qml"));
layout->addWidget(container);
mainwindow->setLayout(layout);
mainwindow->show();
return a.exec();
}
webview.qml
import QtQuick 2.2
import QtWebView 1.0
Item {
visible: true
anchors.fill: parent
WebView {
anchors.fill: parent
url: "https://www.google.de"
}
}
When I run this mini program on iphonesimulator in QtCreator, the layout is like the following screenshots (sorry I can't embed the pictures yet):
We can see the container doesn't fill the top area. And even when the parent widget is much smaller (in my actual program), the container has always this same size. When I rotate the iphonesimulator, it looks then like this!.
Could anyone please help to solve this? It's annoying me for months. Thanks!
I am trying to add a label to the main window using Qt. Here is a piece of the code:
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QWidget Main_Window;
QPixmap Image;
Image.load("1837.jpg");
QLabel i_label;
i_label.setPixmap(Image);
i_label.show();
QPushButton Bu_Quit("Quit", &Main_Window);
QObject::connect(&Bu_Quit, SIGNAL(clicked()), qApp, SLOT(quit()));
Main_Window.show();
return app.exec();
}
I've been having a very hard time figuring out how to properly add QLabels to QWidgets, I tried to set the Main_Window as the main widget using this method: app.setMainWidget(Main_Window) and the label was still outside the window. So how do I put labels into widgets using Qt?
hamza, this code worked fine for me:
#include <QtGui>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget Main_Window;
QLabel i_label("Start", &Main_Window);
//i_label.setPixmap(QPixmap("1837.jpg"));
QPushButton Bu_Quit("Quit" , &Main_Window);
QObject::connect(&Bu_Quit , SIGNAL(clicked()), qApp , SLOT(quit()));
QVBoxLayout *vbl = new QVBoxLayout(&Main_Window);
vbl->addWidget(&i_label);
vbl->addWidget(&Bu_Quit);
Main_Window.show();
return app.exec();
}
I commented setting the image code to show you that the label was set correctly. Make sure your image is valid (otherwise you won't see the text). The trick here was that you need to use qt layouts like QVBoxLayout
Add the label to a layout widget and set the window layout to that layout.
Design note: its better to create your own MainWindow class, inheriting from QMainWindow for instance, and design it from the inside.
or even better, use QtCreator.
You can try:
QWidget window;
QImage image("yourImage.png");
QImage newImage = image.scaled(150, 150, Qt::KeepAspectRatio);
QLabel label("label", &window);
label.setGeometry(100, 100, 100, 100);
label.setPixmap(QPixmap::fromImage(newImage));
window.show();
this way you can even decide where to put the label and choose the image size.