transparent widget in QT - c++

I want to draw a circle on a widget and within the circle background screen should be visible i.e. just border/circumference of circle should be visible on the widget.
Rest of the widget should be transparent.
I have tried :
1) setStyleSheet("background:transparent;")
2) setAttribute( Qt::WA_TranslucentBackground, true );
3) Overriding paintEvent(QPaintEvent* event);
All of the above methode didn't work. and setAutoFillBackground() is false.
I am new to Qt and above solution I just found on google.
Please Help.

The QWidget::setMask function, if used on the window, will have that affect. See also the shaped clock example (also referenced from the setMask documentation).

This will do the job :D
#include <QApplication>
#include <QtGui/QMainWindow>
#include <QtGui/QPushButton>
#include <QtGui/QHBoxLayout>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMainWindow *win=new QMainWindow(0);
win->setAttribute(Qt::WA_TranslucentBackground,true);
win->show();
return a.exec();
}

Related

Expanding QChartView

A little at lost as to why QChartView will expand when put inside of a QTabWidget.
Here's a picture of the application when QChartView is not expanding (because it's hidden).
The black portion of the app is QOpenGLWidget.
When I click on the chart view, it will gradually increase in size until QOpenGLWidget is hidden.
When QChartView is just in a QVBoxLayout with QOpenGLWidget, then this effect does not occur. It's only when I add QChartView inside of the QTabWidget that this happens. I'm trying to figure out how to have QChartView not expand, and resize the same way other widgets do (such as the QTextEdit widget in this example).
Here's the code, which was written as a minimal example to reproduce the effect.
#include <QApplication>
#include <QChart>
#include <QChartView>
#include <QMainWindow>
#include <QOpenGLWidget>
#include <QTabWidget>
#include <QTextEdit>
#include <QVBoxLayout>
int
main(int argc, char** argv)
{
QApplication app(argc, argv);
// Main Window
QMainWindow main_window;
main_window.resize(1280, 720);
main_window.show();
// Central Widget
QWidget central_widget(&main_window);
main_window.setCentralWidget(&central_widget);
QVBoxLayout layout(&central_widget);
central_widget.setLayout(&layout);
// OpenGL Widget
QOpenGLWidget gl_widget(&central_widget);
gl_widget.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
layout.addWidget(&gl_widget);
// Tab Widget
QTabWidget tab_widget(&central_widget);
layout.addWidget(&tab_widget);
// Log
QTextEdit text_edit(&tab_widget);
text_edit.setReadOnly(true);
tab_widget.addTab(&text_edit, "Log");
// Chart View
QtCharts::QChartView chart_view(&tab_widget);
tab_widget.addTab(&chart_view, "Chart");
return app.exec();
}
The problem is caused because the QChartView has the expansion sizePolicy as opposed to the QOpenGLWidget, so when it becomes visible it expands, hiding the other widget. The solution is to set a stretch factor associated with each widget in the layout:
layout.addWidget(&gl_widget, 1);
layout.addWidget(&tab_widget, 1);

Dark theme does not apply to the title bar

I have applied a dark style to my application, so good until then, my question is why the style is not applied to the title bar of my application, and the rest of the forms that open in me application, as you can see, it stays white and looks very bad, any suggestions would be appreciated.
#include "mainwindow.h"
#include <QApplication>
#include <QFile>
#include <QTextStream>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QFile f(":/qdarkstyle/style.qss");
f.open(QIODevice::ReadOnly|QIODevice::Text);
QTextStream ts(&f);
a.setStyleSheet(ts.readAll());
MainWindow w;
w.show();
return a.exec();
}
I got the subject from here.
https://github.com/ColinDuquesnoy/QDarkStyleSheet
[Works on Qt 6.2.2, not sure about Qt5]
There might be a configuration file qt.conf in the same folder as your executable file. If it does not exist, create. And add the lines
[Platforms]
WindowsArguments = darkmode=1
Use darkmode=2 if you also want to change the title bar colour on the fly.

Open widget in another window on macOS

The app opens another widget as a pane. I guess this follows macOS guidelines but is it possible to force widget to open in another window?
Like this:
#include <QApplication>
#include <QMainWindow>
int main(int argc, char *argv[]) {
QApplication a(argc, argv);
QMainWindow w;
w.setWindowTitle("Main Widget");
w.show();
QWidget anotherWidget;
anotherWidget.setWindowTitle("Another Widget");
anotherWidget.show();
return a.exec();
}
To do that, you need to set the flag of the second widget to Qt::Dialog
QWidget anotherWidget;
anotherWidget.setWindowTitle("Another Widget");
anotherWidget.setWindowFlag(Qt::Dialog);
anotherWidget.show();
Open the anotherWidget as a QMainWindow type instead of QWidget.

C++ Qt How do you make a label display a movie (gif) that you created using the form?

I have created a label in the form UI and I would like to make it display a GIF using code in main. This is what I have so far but I'm not sure what to put in place of the "???????". How can I make this label display a GIF?
#include "dialog.h"
#include <QApplication>
#include <QMovie>
#include <QLabel>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Dialog w;
QMovie *movie = new QMovie(":/Gifs/Welcome_animation2.gif");
QLabel *lblMovie = new QLabel();
lblMovie->setMovie(??????????);
movie->start();
w.show();
return a.exec();
}
As pointed by CoryKramer, you should just use lblMovie->setMovie(movie). However, the problem seens to be that you're not setting the movie to the correct QLabel (ie. lblMovie is not the same object shown on your window)
You probably have to create the QMovie inside your Dialog class, and there you set the movie to the QLabel by using ui->lblMovie->setMovie(movie). If it really must be created outside, then you should add a public method to allow it.
Take a look at http://doc.qt.io/qt-5/gettingstartedqt.html for more information about proper usage of ui files

Creating a QVideoWidget in Qt5

I have the following piece of code:
#include <QtWidgets/QtWidgets>
#include <QtMultimedia/QCamera>
#include <QtMultimedia/QMediaPlayer>
int main(int argc, char * argv[])
{
QApplication testQt(argc, argv);
QMainWindow w;
QWidget videoContainer(&w);
w.setCentralWidget(&videoContainer);
QVideoWidget videoWidget(&videoContainer);
QCamera cam(&w);
cam.setViewfinder(&videoWidget);
cam.start();
w.show();
return testQt.exec();
}
in which I am trying to create a main window, create a container widget to display video, create a videowidget in that container, and then finally set the viewfinder of the camera to that videowidget. However, when I try to do this I get the error
Variable has incomplete type 'QVideoWidget'
Why am I getting this error?
You need to include the corresponding header as follows:
#include <QVideoWidget>
You may also need to add this to your project file:
QT += multimediawidgets