Here's a quick example.
This is just to demonstrate how you can create a custom label and paint on top of the existing content.
I created a new standard gui project with creator.
Then added two new classes:
1. Planning, based on QScrollArea
2. CustomLabel, based on QLabel
I added the label to the scrollarea from within the Planning class itself. To me this seems logical as I guess the Planning class will tell the CustomLabel what to do.
If not, you can add the CustomLabel to the Planning scroll area from within your main window too, if that makes it easier for you.
mainwindow.h
#include <QMainWindow>
#include "planning.h"
namespace Ui {
class MainWindow;
}
Q_OBJECT
public:
~MainWindow();
protected:
private:
Ui::MainWindow *ui;
Planning *myPlanning;
};
#endif // MAINWINDOW_H
#include <QMainWindow>
#include "planning.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow {
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
protected:
void changeEvent(QEvent *e);
private:
Ui::MainWindow *ui;
Planning *myPlanning;
};
#endif // MAINWINDOW_H
To copy to clipboard, switch view to plain text mode
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QGridLayout>
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
myPlanning = new Planning(this);
layout->addWidget(myPlanning);
ui->centralWidget->setLayout(layout);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow
::changeEvent(QEvent *e
) {
switch (e->type()) {
ui->retranslateUi(this);
break;
default:
break;
}
}
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QGridLayout>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QGridLayout *layout = new QGridLayout;
myPlanning = new Planning(this);
layout->addWidget(myPlanning);
ui->centralWidget->setLayout(layout);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::changeEvent(QEvent *e)
{
QMainWindow::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}
To copy to clipboard, switch view to plain text mode
planning.h
#ifndef PLANNING_H
#define PLANNING_H
#include <QWidget>
#include <QScrollArea>
#include "customlabel.h"
{
Q_OBJECT
public:
explicit Planning
(QWidget *parent
= 0);
signals:
public slots:
private:
CustomLabel *customLabel;
};
#endif // PLANNING_H
#ifndef PLANNING_H
#define PLANNING_H
#include <QWidget>
#include <QScrollArea>
#include "customlabel.h"
class Planning : public QScrollArea
{
Q_OBJECT
public:
explicit Planning(QWidget *parent = 0);
signals:
public slots:
private:
CustomLabel *customLabel;
};
#endif // PLANNING_H
To copy to clipboard, switch view to plain text mode
planning.cpp
#include "planning.h"
#include <QPixmap>
Planning
::Planning(QWidget *parent
) :{
customLabel = new CustomLabel(this);
setWidget(customLabel);
QPixmap pixmap
(...
);
// use your pixmap here customLabel->setPixmap(pixmap);
customLabel->resize(pixmap.size());
}
#include "planning.h"
#include <QPixmap>
Planning::Planning(QWidget *parent) :
QScrollArea(parent)
{
customLabel = new CustomLabel(this);
setWidget(customLabel);
QPixmap pixmap(...); // use your pixmap here
customLabel->setPixmap(pixmap);
customLabel->resize(pixmap.size());
}
To copy to clipboard, switch view to plain text mode
customlabel.h
#ifndef CUSTOMLABEL_H
#define CUSTOMLABEL_H
#include <QLabel>
class CustomLabel
: public QLabel{
Q_OBJECT
public:
explicit CustomLabel
(QWidget *parent
= 0);
signals:
public slots:
protected:
};
#endif // CUSTOMLABEL_H
#ifndef CUSTOMLABEL_H
#define CUSTOMLABEL_H
#include <QLabel>
class CustomLabel : public QLabel
{
Q_OBJECT
public:
explicit CustomLabel(QWidget *parent = 0);
signals:
public slots:
protected:
void paintEvent(QPaintEvent *);
};
#endif // CUSTOMLABEL_H
To copy to clipboard, switch view to plain text mode
customlabel.cpp
#include "customlabel.h"
#include <QPainter>
#include <QPaintEvent>
#include <QPoint>
#include <QPen>
CustomLabel
::CustomLabel(QWidget *parent
) :{
}
{
// Assuming you want to paint on top of the existing content of the label.
// First draw the label
painter.setPen(pen);
painter.drawLine(start, end);
}
#include "customlabel.h"
#include <QPainter>
#include <QPaintEvent>
#include <QPoint>
#include <QPen>
CustomLabel::CustomLabel(QWidget *parent) :
QLabel(parent)
{
}
void CustomLabel::paintEvent(QPaintEvent *event)
{
// Assuming you want to paint on top of the existing content of the label.
// First draw the label
QLabel::paintEvent(event);
QPainter painter(this);
QPoint start(0,0);
QPoint end(100,100);
QPen pen(qRgb(255,0,0));
painter.setPen(pen);
painter.drawLine(start, end);
}
To copy to clipboard, switch view to plain text mode
Off course you want to rename the CustomLabel class to something more appropriate.
And you want to create some setters and getters to set and get data. Maybe create a list to keep track of data when updating and painting etc...
EDIT:
Ohh, I used a jpg I have on my computer in the code, I edited that out.
Bookmarks