如何运行这个Qt脚本? (新手问题)

时间:2010-03-30 16:39:45

标签: user-interface qt scripting charts

我有一个Qt脚本(barchart.qs),可以为我创建一个图表。我不知道如何在我的Qt主窗口中合并(即显示图表)。有人可以帮我看看代码及其输出是什么?我试过了engine.evaluate,但我不知道我得到的QScriptValue是什么回报。非常感谢。

这是剧本:

BarChart.prototype = new QWidget;
BarChart.prototype.suffix = "";


function BarChart(parent) {

    QWidget.call(this, parent);
}

// find the maximum value and widest (pixel-wise) label and suffix

BarChart.prototype.showEvent = function(event) {
    var fm = new QFontMetrics(this);
    this.margin = 20;
    this.titleHeight = fm.height();
    this.barHeight = 1.5 * fm.height();
    this.barSpacing = 0.6 * fm.height();
    this.maxValue = this.suffixWidth = this.labelWidth = 0;
    var count = 0;

    for (d in this.data) {
        this.labelWidth = Math.max(this.labelWidth, fm.width(d));
        this.maxValue = Math.max(this.maxValue, this.data[d]);
        this.suffixWidth = Math.max(this.suffixWidth, fm.width(String(this.data[d]) + " " + this.suffix));
        count++;
    }

    this.startHue = 15;
    this.hueDelta = 360 / count;
    this.size = new QSize(640, this.titleHeight + 2 * this.margin + (this.barHeight + this.barSpacing) * count);

}



BarChart.prototype.paintEvent = function(event) {

    var p = new QPainter;
    p.begin(this);



    // background and title
    p.fillRect(this.rect, new QBrush(new QColor(255, 255, 255)));
    p.drawText(0, 0, this.width, this.margin + this.titleHeight, Qt.AlignCenter, this.windowTitle, 0);


    var ofs = this.labelWidth + this.margin;
    var ww = this.width - this.suffixWidth - ofs - 2 * this.margin;
    var hue = this.startHue;
    var y = 0;
    p.translate(this.margin, this.titleHeight + 1.5 * this.margin);

    for (d in this.data) {

        // label on the left side
        p.setPen(new QColor(Qt.black));
        p.drawText(0, y, this.labelWidth, this.barHeight, Qt.AlignVCenter + Qt.AlignRight, d, 0);

        // the colored bar
        var gradient = new QLinearGradient(new QPoint(ofs, y), new QPoint(ofs, y + this.barHeight));
        gradient.setColorAt(0, QColor.fromHsv(hue, 255, 240));
        gradient.setColorAt(1, QColor.fromHsv(hue, 255, 92));
        p.setBrush(new QBrush(gradient));
        p.setPen(new QColor(96, 96, 96));
        var bw = this.data[d] * ww / this.maxValue;
        p.drawRect(ofs, y, bw,
        this.barHeight);

        // extra text at the end of the bar
        var text = new String(this.data[d] + " " + this.suffix);
        p.setPen(new QColor(Qt.black));
        p.drawText(ofs + bw + this.margin/2, y, this.suffixWidth, this.barHeight, Qt.AlignVCenter + Qt.AlignLeft, text, 0);

        // for the next bar
        y += (this.barHeight + this.barSpacing);
        hue += this.hueDelta;
        if (hue >= 360)

            hue -= 360;
    }
    p.end();
}

BarChart.prototype.wheelEvent = function(event) {
    this.startHue += event.delta() / 8 / 5;
    if (this.startHue >= 360)
        this.startHue -= 360;

    if (this.startHue < 0)
        this.startHue += 360;
    this.update();
    event.ignore();

}

BarChart.prototype.mousePressEvent = function(event) {
    var fname = QFileDialog.getSaveFileName(this, "Save", ".", "*.png", 0, 0);

    if (fname.length > 0) {
        var img = new QImage(this.size, QImage.Format_ARGB32_Premultiplied);
        this.render(img);
        img.save(new QFile(fname));
    }
}

var chart = new BarChart;
chart.windowTitle = "Monthly";
chart.suffix = "reports";

chart.data = {
          "September" :  45,
          "October" : 60,
        "November" : 56,
"December" : 0
};

chart.show();
QCoreApplication.exec();

1 个答案:

答案 0 :(得分:1)

您可以查看标准Qt SDK附带的 Qt Examples and Demos 应用程序中的简单示例(例如QtScript部分中的计算器)。通过类比这个例子,你可以运行自己的。