qt c++ QChart->setGeometry 在 MainWindow 中不起作用
qt c++ QChart->setGeometry does not work in MainWindow
我创建了一个对象,里面有一个 QChart 和一个带有 2 个 QPushButton 的 MainWindow,当我试图显示我的 QChart 时,它占用了所有 window。如果我想调整 QChart 的大小,一切正常,但如果我尝试移动它,则没有任何工作(使用 setGeometry 或 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();
}
其他文件,我认为不会有用,但如果需要:
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
我想你想看看使用布局。我并不完全乐观,但是让图表成为 window 的中心小部件意味着它将占据所有 space -- 就像它的行为方式一样。
听起来您应该做的是创建一个小部件并将其视为一个容器。它成为您的中央小部件,然后您可以向其中添加内容。但是如果没有布局,您将获得奇怪的调整大小行为。
习惯使用布局而不是硬编码大小和位置来处理布局确实要好得多。您必须使用小部件作为容器才能使事情正常进行,但我尝试做的一切我都能够做到。
我创建了一个对象,里面有一个 QChart 和一个带有 2 个 QPushButton 的 MainWindow,当我试图显示我的 QChart 时,它占用了所有 window。如果我想调整 QChart 的大小,一切正常,但如果我尝试移动它,则没有任何工作(使用 setGeometry 或 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();
}
其他文件,我认为不会有用,但如果需要:
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
我想你想看看使用布局。我并不完全乐观,但是让图表成为 window 的中心小部件意味着它将占据所有 space -- 就像它的行为方式一样。
听起来您应该做的是创建一个小部件并将其视为一个容器。它成为您的中央小部件,然后您可以向其中添加内容。但是如果没有布局,您将获得奇怪的调整大小行为。
习惯使用布局而不是硬编码大小和位置来处理布局确实要好得多。您必须使用小部件作为容器才能使事情正常进行,但我尝试做的一切我都能够做到。