Qt MainWindow ignores width, height and title properties - c++

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

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 !

Using a Designer .ui File in Your Application example not working

I'm trying to build a UI form and interface to it using the example from:
https://doc.qt.io/qt-5/designer-using-a-ui-file.html
#include "ui_item.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget widget;
Ui::Item ui;
ui.setupUi(&widget);
widget.show();
return app.exec();
}
However, it just doesn't work. In the line
ui.setupUi(&widget); I get the error:
invalid conversion from ‘QWidget*’ to ‘QFrame*’
Is there something I'm missing? The top-level widget in Ui::Item is a QFrame. I'm using Qt 5.12 and Qt-Creator 4.12.4.
The issue was that my UI top level was a QFrame and it needed to be a QWidget. For whatever reason, the UI file generated as a QFrame.

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.

QPropertyAnimation not functioning

My animation isn't working on this QPushButton quite the way I'm expecting.
Here's my mainwindow.cpp, as you can see nothing particularly weird here. On runtime the button appears as one might expect. I didn't have any particular movement in mind. I just wanted to see if I had set everything up correctly. As things are right now the button doesn't grow or move. What's strange here is that if I comment out the setShape.start() it defaults back to the size designated in the UI file.
My only guess thus far is that my MainWindow object isn't being displayed until after its constructor (containing the animation) has run its course. If this is the case, I'm wondering what I can do to get around it.
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QLabel>
#include <QPushButton>
#include <QPropertyAnimation>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QPropertyAnimation setShape(ui->pushButton, "geometry");
setShape.setDuration(1000);
setShape.setStartValue(QRect(500,300,500,500));
setShape.setEndValue(QRect(800,400,500,500));
setShape.start();
}
MainWindow::~MainWindow()
{
delete ui;
}
Here's my main.cpp
#include "mainwindow.h"
#include <QApplication>
#include <QPropertyAnimation>
#include <QPushButton>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
This is about as basic an animation as one could create, so I'm hoping that either my intuition on this is correct, or I'm making a silly mistake. Either way any help would be greatly appreciated.
A local variable is deleted after it finishes executing its scope, and that is what happens with setShape, it is eliminated when the constructor finishes executing, what you have to do is create a pointer so that it is maintained and establish the policy of destruction to DeleteWhenStopped:
QPropertyAnimation *setShape = new QPropertyAnimation(ui->pushButton, "geometry");
setShape->setDuration(1000);
setShape->setStartValue(QRect(500,300,500,500));
setShape->setEndValue(QRect(800,400,500,500));
setShape->start(QPropertyAnimation::DeleteWhenStopped);

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.