当绘制图形的代码放置在构造函数中时,图形绘制得很好,但是当使用pushButton槽中的相同代码绘制图形时,图形仅在窗口大小改变时绘制
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
int month[12]={6 ,9 ,6, 5, 6, 4, 6 ,10, 6, 8, 5 ,5};
QBrush gray(Qt::gray);
ui->customPlot->setBackground(gray);
// create empty bar chart objects:
QCPBars *bar = new QCPBars(ui->customPlot->xAxis, ui->customPlot->yAxis);
bar->setName("barerative");
bar->setPen(QPen(QColor(0, 168, 140).lighter(130)));
bar->setBrush(QColor(0, 168, 140));
// prepare x axis with country labels:
QVector<double> ticks;
QVector<QString> labels;
ticks << 1 << 2 << 3 << 4 << 5 << 6 << 7<<8<<9<<10<<11<<12;
labels << "January" << "February" << "March" << "April" << "May" << "June" << "July"<<"August"<<"September"<<"October"<<"November"<<"December";
QSharedPointer<QCPAxisTickerText> textTicker(new QCPAxisTickerText);
textTicker->addTicks(ticks, labels);
ui->customPlot->xAxis->setTicker(textTicker);
ui->customPlot->xAxis->setRange(0, 8);
ui->customPlot->xAxis->grid()->setVisible(true);
ui->customPlot->xAxis->setTickLabelColor(Qt::black);
ui->customPlot->xAxis->setTickLabelFont(QFont("Times",14,2));
// prepare y axis:
ui->customPlot->yAxis->setRange(0, 15);
ui->customPlot->yAxis->setLabel("Total Sale");
ui->customPlot->yAxis->setBasePen(QPen(Qt::white));
ui->customPlot->yAxis->setTickPen(QPen(Qt::white));
ui->customPlot->yAxis->setSubTickPen(QPen(Qt::white));
ui->customPlot->yAxis->grid()->setSubGridVisible(true);
ui->customPlot->yAxis->setTickLabelColor(Qt::white);
ui->customPlot->yAxis->setLabelColor(Qt::white);
ui->customPlot->yAxis->grid()->setPen(QPen(QColor(130, 130, 130), 0, Qt::SolidLine));
ui->customPlot->yAxis->grid()->setSubGridPen(QPen(QColor(130, 130, 130), 0, Qt::DotLine));
// Add data:
QVector<double> Data;
Data<<month[0]<<month[1]<<month[2]<<month[3]<<month[4]<<month[5]<<month[6]<<month[7]<<month[8]<<month[9]<<month[10]<<month[11];
bar->setData(ticks, Data);
}
答案 0 :(得分:0)
在函数结束时调用QCustomGraph上的重绘。不知道确切的电话是什么。
当窗口调整大小时它起作用的原因是当调整大小完成时它会重新绘制视图。