Question

I have the most basic of Qt programs - a main window. I want to be able to use QPainter to draw some lines - nothing more than lines really (I'm trying to plot a histogram). Anyway I've read code examples but for some reason my code isn't working.

I have a mainwindow.ui which creates a class called MainWindow which I haven't shown - it's just a QMainWindow with a QWidget on it but I'm hiding the QWidget as I haven't used it now. main.cc is not shown too because it's the standard main.cc for a small project. The rest of the code is:

mainwindow.cc

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QWidget>
#include <QPainter>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent)
{
    setupUi(this);
    drawingArea->hide();

    QPainter painter(this);
    painter.begin(this);
    QColor color (100, 100, 100);
    painter.setBrush(color);
    painter.drawLine(0,0,10,10);
}

MainWindow::~MainWindow()
{
}

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include "ui_mainwindow.h"

namespace Ui {
    class MainWindow;
}

class MainWindow : public QMainWindow, private Ui::MainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
};

#endif // MAINWINDOW_H
Was it helpful?

Solution

you need to do painting in paintEvent.

see: http://doc.trolltech.com/4.6/widgets-analogclock.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top