[英]Qt - Display a QChartView in a QDialog window
I have been following this example to try create a line chart in a QDialog window. 我一直在遵循此示例 ,尝试在QDialog窗口中创建折线图。 Instead of adding the chart to the UI with:
代替使用以下方法将图表添加到UI:
window.setCentralWidget(chartView);
I am adding it to the UI with this line: 我将其添加到用户界面的这一行:
QChartView *chartView = new QChartView(chart, ui->widget_chart);
I have a QWidget called widget_chart added to my UI file and a horizontal layout applied. 我将一个名为widget_chart的QWidget添加到我的UI文件中,并应用了水平布局。 The chart is shown but it is very small.
该图表已显示,但非常小。 I would have expected that by applying the layout, the chart would take up the full width of the window.
我希望通过应用布局,图表将占据窗口的整个宽度。 And that it would be dynamically resize if I resized the window.
如果我调整窗口大小,它将动态地调整大小。
How can I make it so that the chart takes up the full width of the QWidget? 如何使图表占据QWidget的整个宽度?
Here is the full code for my dialog.cpp file: 这是我的dialog.cpp文件的完整代码:
#include "dialog.h"
#include "ui_dialog.h"
#include <QtCharts>
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
QLineSeries *series = new QLineSeries();
series->append(0, 6);
series->append(2, 4);
series->append(3, 8);
series->append(7, 4);
series->append(10, 5);
QChart *chart = new QChart();
chart->legend()->hide();
chart->addSeries(series);
chart->createDefaultAxes();
chart->setTitle("Simple line chart example");
QChartView *chartView = new QChartView(chart, ui->widget_chart);
chartView->setRenderHint(QPainter::Antialiasing);
}
Dialog::~Dialog()
{
delete ui;
}
The answer by Wojciech Piątek worked for me. WojciechPiątek的回答对我有用 。 As far as A_Noonan is concerned the only thing you need to change is to find out what is the main layout in your ui file and reference it like this
就A_Noonan而言,您唯一需要更改的就是找出ui文件中的主要布局,并像这样引用它
ui->main_layout->addWidget(chartView);
here you should change main_layout to whatever your layout name is. 在这里,您应该将main_layout更改为您的布局名称。 Also doing this way you don't need to have this line:
同样,您也不需要这样:
QChartView *chartView = new QChartView(chart, ui->widget_chart);
anymore, instead you can use the regular one. 现在,您可以使用常规的。
QChartView *chartView = new QChartView(chart);
also make sure to delete the widget_chart from the ui_file. 还请确保从ui_file中删除widget_chart。 There is no other way around this.
没有其他方法可以解决此问题。
Just change the size of the chart view, I am using PyQt5 but I think they have the same syntax for the library. 只是更改图表视图的大小,我正在使用PyQt5,但我认为它们的库语法相同。
chartView = QChartView(chart, self)
chartView.setRenderHint(QtGui.QPainter.Antialiasing)
chartView.resize(self.size())
mainLayout = QVBoxLayout()
mainLayout.addWidget(chartView, 5)
Instead of using self, you would use the 'this' operator. 除了使用self之外,您还可以使用'this'运算符。
chartView.resize(this->size())
I had similar problem. 我有类似的问题。 I resolved it using layouts.
我使用布局解决了它。 Create layout for your
QWidget
, and then add QChartView
using addWidget
function: 为您的
QWidget
创建布局,然后使用addWidget
函数添加QChartView
:
your_layout->addWidget(chartView)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.