开发者

How to run this Qt script? (newbie question)

I have a Qt script (barchart.qs) that creates a graph for me. I don't know how I can incorporate (i.e. show the graph) in my Qt mainwindow. Can someone please help me look at the code and what its outputs are? I tried engine.evaluate, but I don't know what is the QScriptValue I'm getting in return. Thanks sooo much.

This is the script:

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.开发者_C百科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();


You may look simple examples from the Qt Examples and Demos application which comes with standard Qt SDK(e.g. Calculator in QtScript section). By analogy with this examples you could run your own.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜