How to inherit structure ?
-
I inherit the Qwidget class with the uptk structure, the Qwidget class.
To call elements from structures
wgt->_myTexpr.m_pitch, wgt->_myTexpr.substrate, wgt->_myTexpr.outline, wgt->_myTexpr.m_t_razm[10]
Writes an error
wgt_screen_turbo/examples/mainwindow.cpp:27: ошибка: '_myTexpr' is a protected member of 'uptk::wgt_screen'
How to fix this error ??
here is the code:
mainwindow.h
#ifndef MAINWINDOWS_H #define MAINWINDOWS_H #include <QWidget> #include <QGridLayout> #include <QDebug> #include <QApplication> #include <QSize> #include <QScreen> #include "../src/wgt_screen.h" class mainwindow : public QWidget { Q_OBJECT public: explicit mainwindow(QWidget *parent = nullptr); signals: private: QGridLayout* gridlayout; uptk::wgt_screen* wgt; protected: void keyPressEvent(QKeyEvent *event); }; #endif // MAINWINDOWS_H
mainwindow.cpp
#include "mainwindow.h" mainwindow::mainwindow(QWidget *parent) : QWidget{parent} , wgt (new uptk::wgt_screen(this)) , gridlayout (new QGridLayout(parent)) { QRect size = QGuiApplication::primaryScreen()->geometry(); this->resize(size.width() * 0.25, size.height() * 0.25); gridlayout->addWidget(wgt); this->setLayout(gridlayout); } void mainwindow::keyPressEvent(QKeyEvent *event) { switch (event->key()) { case Qt::Key_A: // фон wgt->_myTexpr.m_pitch; if(wgt->_myTexpr.substrate) { wgt->setSubstrate(false, 48, 172, 220, 120); } else { wgt->setSubstrate(true, 48, 172, 220, 120); } break; case Qt::Key_S: // обводы шрифта qDebug() << "_myTexpr.outline Key_S" << wgt->_myTexpr.outline; if(wgt->_myTexpr.outline) { wgt->setOutline(false); } else { wgt->setOutline(true); } break; case Qt::Key_X: wgt->_myTexpr.m_t_razm[10] = wgt->_myTexpr.m_t_razm[10]+1; if(wgt->_myTexpr.m_t_razm[10]>3) { wgt->_myTexpr.m_t_razm[10] = 0; } break; case Qt::Key_Z: wgt->_myTexpr.m_t_razm[10] = wgt->_myTexpr.m_t_razm[10]-1; if(wgt->_myTexpr.m_t_razm[10]<0) { wgt->_myTexpr.m_t_razm[10] = 3; } break; default: QWidget::keyPressEvent(event); break; } update(); }
wgt_screen.h
#ifndef WGT_SCREEN_H #define WGT_SCREEN_H #include <QWidget> #include <QDebug> #include <QPainter> #include <QPen> #include <QFont> #include <QFontMetrics> #include <QKeyEvent> #include <QRegion> #include <QPointF> #include <QGraphicsView> #include <QtMath> #include <QRectF> #include <QPainterPath> #include <QFontMetrics> namespace uptk { struct mtRazm { float paddingX; float paddingY; }; struct texnStructur { texnStructur() { value = 0; substrate = false; outline = false; substrate_t = false; for(uint i=0; i < sizeof(rgb)/ sizeof(rgb[0]); i++) rgb[i] = 0; screen_size_x = 0; screen_size_y = 0; for(uint i=0; i < sizeof(m_t_razm)/ sizeof(m_t_razm[0]); i++) m_t_razm[i] = 0; } float m_t_razm[14]; mtRazm razm; int screen_size_x; int screen_size_y; int m_pitch; QPainterPath path; float value; bool substrate; bool substrate_t; int rgb[4]; bool outline; QString title; QString nadp2; QString nadp3; }; class wgt_screen : public QWidget { Q_OBJECT public: wgt_screen(QWidget *parent = nullptr); ~wgt_screen(); void setSubstrate(bool val); void setSubstrate(bool val, int rgb0, int rgb1, int rgb2, int rgb3 ); void setOutline(bool val); void size_wgt_line(int x, int y); protected: void paintEvent(QPaintEvent *event); void keyPressEvent(QKeyEvent *event); texnStructur _myTexpr; }; } // namespace uptk #endif // WGT_SCREEN_H
-
@timob256 said in How to inherit structure ?:
How to fix this error ??
By learning c++ basics?
If you want to access a private member of a class (for whatever reason this should be needed) you should write a getter/setter function for it. -
@timob256 First of all, I recommend against using protected mode. It is a subtle violation of the Liskov Substitution Principle that will bite you (or whomever gets stuck with the maintenance of this code).
The immediate cure is to add an accessor in the public section of class wgt_screen:
public: ... texnStructur getMyTexpr() {return _myTexpr;} ...
Then, wherever you use_myTexpr.[fieldname] in the main window, use getMyTexpr().[fieldname] instead.
Example:
if(wgt->_myTexpr.outline)
would become
if(wgt->getMyTexpr().outline)