I created an object with a QChart inside and a MainWindow with 2 QPushButton, when I try to display my QChart, it takes all window. If I want to resize QChart everything works but if I try to move it nothing work (with setGeometry or with setContentMargin).
main.cpp
#include <QApplication>
#include "Mainwindow.h"
#include "MainChart.h"
int main(int argc, char **argv)
{
QApplication app(argc, argv);
MainWindow w(1920, 1080);
MainChart chart;
chart.setGeometry(250,0,1670,1080); // >> KO
w.setCentralWidget(chart.get_view());
// w.centralWidget()->setMaximumSize(1670, 1080); >> OK
// w.centralWidget()->setContentsMargins(250,0,0,0); >> KO
w.show();
return app.exec();
}
Other files, I think that won't be useful but if need:
MainWindow.cpp
#include <QMainWindow>
#include <QWidget>
#include <QtGui>
#include <QAction>
#include <Qt>
#include <QKeySequence>
#include <QtCore>
#include <iostream>
#include "Mainwindow.h"
MainWindow::MainWindow(int _width, int _height, QMainWindow *parent)
: width(_width), height(_height), QMainWindow(parent)
{
this->setStyleSheet("MainWindow {background-color: rgb(40,40,40);}");
this->setWindowFlags( Qt::CustomizeWindowHint );
this->showFullScreen();
this->setFixedSize(width, height);
configure_new_button();
configure_exit_button();
configure_escape();
}
MainWindow::~MainWindow()
{
free(exit_btn);
free(new_btn);
free(escape);
}
void MainWindow::configure_exit_button()
{
exit_btn = new QPushButton(this);
exit_btn->connect(exit_btn, SIGNAL(clicked()),this, SLOT(exit()));
exit_btn->setGeometry(0, height - 100, 100, 100);
exit_btn->setStyleSheet("QPushButton {background-color: rgb(150,150,150);}");
QFont font = exit_btn->font();
font.setPointSize(32);
exit_btn->setFont(font);
exit_btn->setIcon(QIcon(":/Icons/close.png"));
exit_btn->setIconSize(QSize(65, 65));
exit_btn->show();
}
void MainWindow::configure_new_button()
{
new_btn = new QPushButton(this);
new_btn->setGeometry(0, 0, 100, 100);
new_btn->connect(new_btn, SIGNAL(clicked()),this, SLOT(new_entry()));
new_btn->setStyleSheet("background-color: rgb(0,0,200);" "color: black");
QFont font = new_btn->font();
font.setPointSize(32);
new_btn->setFont(font);
new_btn->setIcon(QIcon(":/Icons/nouveau.png"));
new_btn->setIconSize(QSize(65, 65));
new_btn->show();
}
void MainWindow::configure_escape()
{
escape = new QAction( "text4ESC", this );
escape->setShortcut( Qt::Key_Escape );
escape->setShortcutContext( Qt::WindowShortcut );
connect(escape, SIGNAL(triggered()), this, SLOT(exit()));
addAction(escape);
}
void MainWindow::exit()
{
close();
qApp->quit();
}
void MainWindow::new_entry()
{
std::cout << "coucou !!!" << std::endl;
}
Mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QWidget>
#include <QObject>
#include <QPushButton>
#include <QEvent>
#include <QKeyEvent>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(int _width, int _height, QMainWindow *parent = 0);
~MainWindow();
void configure_exit_button();
void configure_new_button();
void configure_escape();
private slots:
void exit();
void new_entry();
private:
QPushButton *exit_btn;
QPushButton *new_btn;
QAction *escape;
int width;
int height;
};
#endif //MAINWINDOW_H
Mainchart.cpp
#include "MainChart.h"
MainChart::MainChart(QChart *Parent)
{
QBarSet *set0 = new QBarSet("Altuve");
QBarSet *set1 = new QBarSet("Martine");
QBarSet *set2 = new QBarSet("Bob");
*set0 << 256 << 954 << 752 << 148 << 596 << 214;
*set1 << 586 << 369 << 485 << 874 << 693 << 587;
*set2 << 785 << 963 << 547 << 745 << 657 << 874;
QFont font;
font.setPixelSize(18);
QBarSeries *series = new QBarSeries();
series->append(set0);
series->append(set1);
series->append(set2);
QChart *chart = new QChart();
chart->addSeries(series);
chart->setTitle("Mon Super Graphique");
chart->setTitleFont(font);
chart->setTitleBrush(QBrush(qRgb(255,255,255)));
chart->setAnimationOptions(QChart::AllAnimations);
QStringList categories;
categories << "2013" << "2014" << "2015" << "2016" << "2017";
QBarCategoryAxis *axis = new QBarCategoryAxis();
axis->append(categories);
chart->createDefaultAxes();
chart->setAxisX(axis, series);
font.setPixelSize(12);
chart->axisX()->setLabelsBrush(QBrush(qRgb(255,255,255)));
chart->axisX()->setLabelsFont(font);
chart->axisY()->setLabelsBrush(QBrush(qRgb(255,255,255)));
chart->axisY()->setLabelsFont(font);
chart->legend()->setVisible(true);
chart->legend()->setAlignment(Qt::AlignBottom);
chart->legend()->setLabelColor(qRgb(255,255,255));
chart->legend()->setFont(font);
chart->setBackgroundBrush(QBrush(QRgb(0x0)));
chartView = new QChartView(chart);
chartView->setRenderHint(QPainter::Antialiasing);
}
MainChart::~MainChart() {}
QChartView * MainChart::get_view()
{
return chartView;
}
Mainchart.h
#ifndef NEW_MAINCHART_H
#define NEW_MAINCHART_H
#include <QtCharts/QChartView>
#include <QtCharts/QSplineSeries>
#include <QtCharts/QBarSeries>
#include <QtCharts/QBarSet>
#include <QtCharts/QBarCategoryAxis>
QT_CHARTS_USE_NAMESPACE
class MainChart : public QChart
{
public:
MainChart(QChart *Parent = 0);
~MainChart();
QChartView *get_view();
private:
QChartView *chartView;
};
#endif //NEW_MAINCHART_H
I think you want to look at using layouts. I'm not entirely positive, but making the chart the window's central widget means it's going to take up all the space -- just the way it's behaving.
It sounds like what you should do is create a widget and consider it a container. It becomes your central widget, and then you add things to it. But without a layout, you're going to get weird resize behavior.
It's really far better to grow accustomed to handling layouts using a layout rather than hard-coded size & locations. You have to use widgets as containers to make things work, but everything I've tried to do I've been able to do.