Expanding QChartView - c++

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);

Related

QIcon not showing in QToolBar

I'm a beginner in Qt, currently reading this : https://zetcode.com/gui/qt5/menusandtoolbars/
When I declare QActions in a QToolBar, the QPixmap objects (turned into QIcons) are not showing :
No icons, only text
However, the QPixmap images are actually showing when I declare a QMenu without the toolbar.
I am using Qt6 ; working on Fedora ; no warning shown on my compiler.
simple_menu.hpp
#ifndef SIMPLE_MENU_HPP
#define SIMPLE_MENU_HPP
#include <QMainWindow>
#include <QApplication>
class SimpleMenu : public QMainWindow
{
public:
SimpleMenu(QWidget *parent = nullptr);
};
#endif
simple_menu.cpp
#include "simple_menu.hpp"
#include <QMenu>
#include <QMenuBar>
#include <QToolBar>
#include <QIcon>
#include <QAction>
SimpleMenu::SimpleMenu(QWidget *parent)
: QMainWindow(parent)
{
QPixmap newpix("new.png");
QPixmap openpix("open.png");
QToolBar *toolbar = addToolBar("main toolbar");
toolbar->addAction(QIcon(newpix), "New File");
toolbar->addAction(QIcon(openpix), "Open File");
}
main.cpp
#include "simple_menu.hpp"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
SimpleMenu window;
window.resize(350, 250);
window.setWindowTitle("Tuto Toolbar");
window.show();
return app.exec();
}
Maybe the system cannot find your pictures. Please try to put the full file path (like :).
QPixmap newpix("c:\mydoc\pictures\new.png");
To check if the pictures are loaded, you can do something a bit different :
QPixmap newpix;
qDebug() << newpix.load("c:\mydoc\pictures\new.png");
the load method returns true or false in case of loading fail.
Anyway, if you want to embed your icons in the final EXE file, check the Qt resource system
It's a convenient system to achieve what you want to do, and to avoid dealing with storage paths on your local drives.
Good luck !

Qt MainWindow ignores width, height and title properties

I've created a simple Qt Widgets application using Qt Creator on my Windows 10 machine. I use the ui file and the designer to change properties of my QMainWindow, but somehow the width, height and windowTitle properties have no effect when I set them in the designer. Example:
However, the resulting application looks like this:
Both size and windowTitle are seemingly ignored. I've also tried setting properties from code, like this (but to no avail):
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
setWindowTitle("That's some title you've got there");
}
I have also added a layout to the centralWidget but that only had an effect on the child controls, not the actual window itself (and that seems logical).
Normally, it should work.
Can you try if the following minimal example works for you?
#include <QApplication>
#include <QMainWindow>
main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMainWindow m;
m.setWindowTitle("TEST");
m.show ();
a.exec();
}

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

transparent widget in QT

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();
}

QT 4.8 padding in QMainWindow

I'm started studying QT. When I create a MainWindow and placing some widgets on it in ane layout, there is a gap between edge of window and widgets, like that:
How can I switch off this gaps?
layout()->setContentsMargins(0,0,0,0);
and editing stylesheets of window, but there was no effect. What should I do?
A QMainWindow is slightly different than a QDialog or QWidget in that it has the concept of a "central widget". The window has predefined areas to handle stuff like toolbars and menus and docks, and defines the central widget as the main content for the window. The window itself is not usually assigned a layout. But what I assume you are doing is setting the values on the windows layout (which will not have an effect).
The widget you set as the central widget will most likely have its own layout. By default the central widget can expand to the edges already. Consider this example first:
#include <QApplication>
#include <QMainWindow>
#include <QVBoxLayout>
#include <QListWidget>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMainWindow *window = new QMainWindow;
window->resize(800,600);
QListWidget *listWidget = new QListWidget;
window->setCentralWidget(listWidget);
window->show();
return a.exec();
}
You will see the list widget fully expanded to the edges. But in a more realistic example:
#include <QApplication>
#include <QMainWindow>
#include <QVBoxLayout>
#include <QListWidget>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMainWindow *window = new QMainWindow;
window->resize(800,600);
QWidget *central = new QWidget;
QListWidget *listWidget = new QListWidget;
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(listWidget);
//Uncomment this following line to remove margins
//layout->setContentsMargins(0,0,0,0);
central->setLayout(layout);
window->setCentralWidget(central);
window->show();
return a.exec();
}
You have a container widget, which is then composed with a layout, and the list widget. The layout of this central widget is the one that introduces the margins.