我基本上是在嘗試在 Qt 中制作甘特圖。我打算在影像上繪制條形圖。我可以在圖表的開頭繪制一個條形(綠色),但我也可以在靠近圖表末尾的地方繪制第二個條形(綠色)。我認為 Chart 的可繪制區域沒有填滿整個 ChartView,因此它正在對缺失的資料進行某種重復填充(可能?)。希望有人能告訴我我做錯了什么。QT 版本 5.15.0。
ui.setupUi(parent);
QGridLayout *gridLayout = new QGridLayout(ui.widget);
//Build a series to add to the chart
QtCharts::QLineSeries *series = new QtCharts::QLineSeries();
*series << QPointF(11, 1) << QPointF(13, 3) << QPointF(17, 6) << QPointF(18, 3) << QPointF(20, 2);
//Create a chart object and format
QtCharts::QChart *chart = new QtCharts::QChart();
chart->legend()->hide();
chart->addSeries(series);
chart->createDefaultAxes();
chart->axes(Qt::Vertical).first()->setGridLineVisible(false);
chart->axes(Qt::Horizontal).first()->setGridLineVisible(false);
// ===== CHART VIEWER ===== //
// Create a Chart Viewer:
QtCharts::QChartView* chartView = new QtCharts::QChartView();
// Add widget to GUI:
gridLayout->addWidget(chartView);
//Add the chart to the view widget
chartView->setChart(chart);
chartView->setRenderHint(QPainter::Antialiasing, true);
//Note: Since we added the chart to the view we can now
//scale and translate the image appropriately. The chart
//has an appropriate size.
//Grab the size of the plot and view areas
chart->setPlotArea(QRectF());
int width = static_cast<int>(chart->plotArea().width());
int height = static_cast<int>(chart->plotArea().height());
int ViewW = static_cast<int>(chartView->width());
int ViewH = static_cast<int>(chartView->height());
//We have to translate the image because setPlotAreaBackGround
//starts the image in the top left corner of the view not the
//plot area. So, to offset we will make a new image the size of
//view and offset our image within that image with white
QImage translated(ViewW, ViewH, QImage::Format_ARGB32);
translated.fill(Qt::gray);
QPainter painter(&translated);
QPointF TopLeft = chart->plotArea().topLeft();
QPointF BotRight = chart->plotArea().bottomRight();
painter.fillRect(TopLeft.x(), TopLeft.y(), 10, BotRight.y() - TopLeft.y(), Qt::green);
//Display image in background
chart->setPlotAreaBackgroundBrush(translated);
chart->setPlotAreaBackgroundVisible(true);

uj5u.com熱心網友回復:
我做了兩個改變:
- 在槽函式中繪制背景。
- 發出 slotAreaChanged 信號。
這是代碼
#include<QGridLayout>
#include<QLineSeries>
#include<QChartView>
#include<QChart>
#include<QApplication>
int main()
{
int a = 0;
QApplication b(a, nullptr);
QWidget w;
QGridLayout* gridLayout = new QGridLayout();
//Build a series to add to the chart
QtCharts::QLineSeries* series = new QtCharts::QLineSeries();
*series << QPointF(11, 1) << QPointF(13, 3) << QPointF(17, 6) << QPointF(18, 3) << QPointF(20, 2);
//Create a chart object and format
QtCharts::QChart* chart = new QtCharts::QChart();
chart->legend()->hide();
chart->addSeries(series);
chart->createDefaultAxes();
chart->axes(Qt::Vertical).first()->setGridLineVisible(false);
chart->axes(Qt::Horizontal).first()->setGridLineVisible(false);
// ===== CHART VIEWER ===== //
// Create a Chart Viewer:
QtCharts::QChartView* chartView = new QtCharts::QChartView();
// Add widget to GUI:
gridLayout->addWidget(chartView);
//Add the chart to the view widget
chartView->setChart(chart);
chartView->setRenderHint(QPainter::Antialiasing, true);
//Note: Since we added the chart to the view we can now
//scale and translate the image appropriately. The chart
//has an appropriate size.
//Grab the size of the plot and view areas
chart->setPlotArea(QRectF());
QObject::connect(chart, &QtCharts::QChart::plotAreaChanged, [&](const QRectF& plotArea) {
int width = static_cast<int>(chart->plotArea().width());
int height = static_cast<int>(chart->plotArea().height());
int ViewW = static_cast<int>(chartView->width());
int ViewH = static_cast<int>(chartView->height());
//We have to translate the image because setPlotAreaBackGround
//starts the image in the top left corner of the view not the
//plot area. So, to offset we will make a new image the size of
//view and offset our image within that image with white
QImage translated(ViewW, ViewH, QImage::Format_ARGB32);
translated.fill(Qt::gray);
QPainter painter(&translated);
QPointF TopLeft = chart->plotArea().topLeft();
QPointF BotRight = chart->plotArea().bottomRight();
painter.fillRect(TopLeft.x(), TopLeft.y(), 10, BotRight.y() - TopLeft.y(), Qt::green);
//Display image in background
chart->setPlotAreaBackgroundBrush(translated);
chart->setPlotAreaBackgroundVisible(true);
});
w.setLayout(gridLayout);
chart->plotAreaChanged(QRectF());
w.show();
return b.exec();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/349138.html
