I from defines a class (fool), join the QTabwidget in the main window (widget), under the QTabwidget has two TAB, I under the first TAB (widget1) to join the procession of the custom class (fool, in the main window name is shabi), custom class writing, I have the drawing of related functions, but the program runs according to not to come out, what it is to validate the custom classes offered and the main window of the widget code is as follows:
* some code with comments will be a little behind on the numerical error, please don't worry about it
custom class fool. H
# # ifndef FOOL_H
# define FOOL_H
# include
# include & lt; QWidget>
# include
# include
The class fool: public QWidget
{
Q_OBJECT
Public:
Explicit fool (QWidget * parent=nullptr);
Void XYPaint ();
Private:
QImage image;
Protected:
Void paintEvent (QPaintEvent *)
{
QPainter painter(this);
Painter. DrawImage (20, 40, image);
}
Signals:
};
# endif//FOOL_H
custom class fool. CPP:
H # include "fool."
# include
# include
# include
Fool: : fool (QWidget * parent) : QWidget (parent)
{
BTN//QPushButton *=new QPushButton (this);
//BTN - & gt; 50,50,100,100 setGeometry ();
//create a QPainter statistical coordinate system to achieve
Image=QImage (400400, QImage: : Format_RGB32); Initialization of//canvas size is 400 * 400, using 32-bit color
,20,255 QColor backColor=qRgb (255);//canvas initialization background color with white
Image. The fill (backColor);//to fill the canvas
XYPaint ();
}
Void fool: : XYPaint ()
{
QPainter painter (& amp; Image);
painter.setRenderHint(QPainter::Antialiasing, true);//set the sawtooth pattern, prettier
Int pointx=20, pointy=375;//sure axis starting point coordinates, defined here (20375)
Int width=480 - pointx, height=360;//sure axis width and height defined above the canvas for 400 x400, highly depends on the wide,
//draw the axis coordinate origin (20375)
Painter. DrawLine (pointx, pointy, width + pointx, pointy);//axis x width is the width
Painter. DrawLine (pointx, pointy - height, pointx, pointy);//axis y height of height
Painter. SetPen (Qt: : black);
,0,520,400 painter. DrawRect (0);//peripheral rectangle
//map scale
QPen penDegree;
PenDegree. SetColor (Qt: : black);
PenDegree. SetWidth (2);
Painter. SetPen (penDegree);
Painter. DrawText (width + 25, height + 20, "number");
Painter. DrawText (pointx - 15, pointy - 360, "the output temperature (K)/p (Pa)");
//to draw on the x axis calibration
for(int i=0; i<40; I++)//into 40 shares
{
//indicate the origin and the point at the end of the
If (I==0)
{painter. DrawText (pointx - 5, pointy + 15, QString: : number (0)); }
If (I==39)
{painter. DrawText (pointx + (I + 0.55) * width/40, pointy + 15, QString: : number (40)); }
//select the appropriate coordinate, draw a length of 2 line, used to represent a scale
Painter. DrawLine (pointx + (I + 1) * width/40, pointy, pointx + (I + 1) * width/40, pointy + 2);
If (I==0 | | (I % 4)==0)
{painter. DrawText (pointx + (I + 0.55) * width/40, pointy + 15, QString: : number (I + 1)); }
}
main window widget. H:
# # ifndef WIDGET_H
# define WIDGET_H
# include
# include
H # include "fool."
QT_BEGIN_NAMESPACE
The namespace Ui {class Widget; }
QT_END_NAMESPACE
The class Widget: public QWidget
{
Q_OBJECT
Public:
The parent Widget (QWidget *=nullptr);
~ the Widget ();
QTabWidget * tabwidget=new QTabWidget (this);
//to make static coordinate system TAB
QWidget * widget1=new QWidget ();
//to make dynamic coordinate system TAB
QWidget * widget2=new QWidget ();
Private:
Ui: Ui widgets *;
Protected:
};
# endif//WIDGET_H
main window widget. CPP:
# include "widget. H"
# include "ui_widget. H"
# include & lt; QTabWidget>
H # include "fool."
Widgets: : widgets (QWidget * parent)
: QWidget (parent)
, the UI (new UI: : Widget)
{
The UI - & gt; SetupUi (this);
//tabwidget in the header file
Tabwidget - & gt; SetFixedSize (600400);
Tabwidget - & gt; ,50,600,400 setGeometry (100);
Tabwidget - & gt; SetVisible (true);
Fool * shabi=new fool (widget1);
Shabi - & gt; Show ();
Tabwidget - & gt; AddTab (widget1, "jingtai");
Tabwidget - & gt; AddTab (widget2, "dongtai");
}
Widgets: : ~ Widget ()
{
Delete the UI;
}