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.
Related
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(¢ral_widget);
QVBoxLayout layout(¢ral_widget);
central_widget.setLayout(&layout);
// OpenGL Widget
QOpenGLWidget gl_widget(¢ral_widget);
gl_widget.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
layout.addWidget(&gl_widget);
// Tab Widget
QTabWidget tab_widget(¢ral_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);
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.
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();
}
I have problems to create a QMainWindow object from a subroutine. I need this to create a different number of windows on the user's request.
The problem lies in the fact that if I create it from a subroutine, it is created as a local variable and only shows for a split second. Usually I would need to return a pointer, but I have not managed to do it with this object. Here is the code:
#include <QApplication>
#include <QMainWindow>
QMainWindow runGUI(){
QMainWindow window;
window.show();
window.resize(340,260);
return window;
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
/*
QMainWindow window;
window.show();
window.resize(340,260);
*/
runGUI();
return app.exec();
}
How do I do this correctly?
Returning your QMainWindow from runGUI() calls the copy constructor: Qt objects are not designed to be copied. You should indeed return a pointer, and dynamically allocate the window:
#include <QApplication>
#include <QMainWindow>
QMainWindow* runGUI(){
QMainWindow* window = new QMainWindow;
window->setAttribute(Qt::WA_DeleteOnClose);
window->show();
window->resize(340,260);
return window;
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QMainWindow* window = runGUI();
return app.exec();
}
Note the use of Qt::WA_DeleteOnClose to ensure the window will be deleted when it is closed, avoiding memory leaks.
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