问题
I am receiving a warning in Qt about setting geometry every time I try to open a new window or Dialog. It even happens with QDialogInput
. And I'm not trying to set the geometry, just calling the window already gives me a warning. Here's an example:
main.cpp
QLineSeries *series = new QLineSeries();
for(int i=0;i<graphPoints;i++)
series->append(i,(qrand()%5)+20);
QChart *chart = new QChart();
chart->legend()->hide();
chart->addSeries(series);
chart->createDefaultAxes();
chart->setTitle("X vs Y teste");
QChartView *chartView = new QChartView(chart);
chartView->setRenderHint(QPainter::Antialiasing);
QDialog *graph = new QDialog(this);
QHBoxLayout *layout = new QHBoxLayout(graph);
layout->addWidget(chartView);
graph->setLayout(layout);
graph->exec();
warning:
setGeometry: Unable to set geometry 116x30+619+380 on QWidgetWindow/'QDialogClassWindow'. Resulting geometry: 116x91+619+380 (frame: 8, 30, 8, 8, custom margin: 0, 0, 0, 0, minimum size: 91x91, maximum size: 16777215x16777215).
This was not a problem until recently I tried to use setGeometry(200,200,200,200)
and nothing happened.
Any idea what this may be?
EDIT
I made a very simple program. Ony included QInputDialog
and called QInputDialog::getString()
. Here's the example:
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QInputDialog>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QInputDialog::getText(this,"title","label");
}
MainWindow::~MainWindow()
{
delete ui;
}
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
output:
Starting C:\Users\socc\Documents\Qt\build-VerySimple-Desktop_Qt_5_10_0_MinGW_32bit-Debug\debug\VerySimple.exe...
setGeometry: Unable to set geometry 116x30+100+105 on QWidgetWindow/'QInputDialogClassWindow'. Resulting geometry: 178x90+100+105 (frame: 8, 30, 8, 8, custom margin: 0, 0, 0, 0, minimum size: 178x90, maximum size: 524287x90).
C:/Users/socc/Documents/Qt/build-VerySimple-Desktop_Qt_5_10_0_MinGW_32bit-Debug/debug/VerySimple.exe exited with code 0
回答1:
In practice, this warning can show for cases like: if using ui form editor and you manually drag a widget such that resulting geometry can't fit in screen at run time, Or more generally, when a widget geometry is smaller than the layout contents, it isn't a serious issue because the widget will automatically adjust and render correctly, but this issue can be solved by insuring the widget gets enough space before rendered, this is done by adjusting its size to contents, thus try:
graph->adjustSize();
graph->exec();
On the other hand, while the issue is not reproducible to me, with graph->setGeometry(200,200,200,200);
but it only appeared at ((200,200,100,100), it could be your geometry width/height is smaller than your chart view dimensions, try to adjust them to higher geometry width/height to accommodate your chart.
回答2:
Regarding the other case in Update where the static was used to create a window with little information see static public members .. you are not setting a window type or any other hint for the dialog.
A widget that is not embedded in a parent widget is called a window. ... it is also possible to create windows without such decoration using suitable window flags)
you need to use the static with either an explicit window type (ex: Qt::Popup
):
QInputDialog::getText(this,"title","label",QLineEdit::Normal,"",nullptr,Qt::Popup);
Or a window hint (ex. Qt::MSWindowsFixedSizeDialogHint
)
QInputDialog::getText(this,"title","label",QLineEdit::Normal,"",nullptr,Qt::MSWindowsFixedSizeDialogHint);
来源:https://stackoverflow.com/questions/49700394/qt-unable-to-set-geometry