PDA

View Full Version : Reaching the information of mainwindow from a class



sasafeb
24th October 2016, 03:38
Hi there

I have a code which makes a simple paint which let the user draw Line, Rectangle, Ellipse ... , and works well, but what I did is to put all the information of different shapes in MainWindow.cpp.
Now I want to edit this code and make a class for each shape, but I donnow how to reach the informaion in MainWindow.cpp from classes.

I tried I thought of making a line class, in the header folder I make no change, but in the .cpp file, I put #include "mainwindow.h", so I suppose information from initUi should be available from my new class, I moved codes related to line action to new class and tried it. But as errors show, including mainwindow as a header file, seems it's not possible to reach to initUi.

I was advised to use SIGNAL/SLOT for this aim, but I really donnow how.

Is there anyone who can help me about making a different class just for Line action?

The code consists of: MainWindow.h & .cpp / paintscene.h & .cpp / drawhistory.h & .cpp

I include MainWindow.cpp, as I believe it's only thing needed and related, but if you want I can include other files too.
I attach a photo of result too.



#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
this->initUi();
this->loadSettings();
}

MainWindow::~MainWindow()
{
this->saveSettings();
}

void MainWindow::initUi()
{
this->setCentralWidget(new QWidget(this));

this->setMenuBar(new QMenuBar(this->centralWidget()));

auto toolBar = new QToolBar(this->centralWidget());
this->addToolBar(toolBar);

this->setStatusBar(new QStatusBar(this->centralWidget()));

_paintScene = new PaintScene(this);

auto *layout = new QVBoxLayout(this->centralWidget());

layout->addWidget(_paintScene);

auto figMenu = this->menuBar()->addMenu(tr("Figures"));
auto aboutMenu = this->menuBar()->addMenu(tr("About"));

//figures
auto lineAction = figMenu->addAction(QIcon(":/img/Resources/Img/line_stroked"), tr("Draw line"));
auto rectAction = figMenu->addAction(QIcon(":/img/Resources/Img/rectangle_stroked"), tr("Draw rect"));
auto ellipseAction = figMenu->addAction(QIcon(":/img/Resources/Img/ellipse_stroked"), tr("Draw ellipse"));
auto polyAction = figMenu->addAction(QIcon(":/img/Resources/Img/polyline"), tr("Draw polyline"));
auto brushAction = figMenu->addAction(QIcon(":/img/Resources/Img/paint_brush"), tr("Free draw"));
figMenu->addSeparator();
auto undoAction = figMenu->addAction(QIcon(":/img/Resources/Img/undo"), tr("Undo"));

//about
auto aboutAction = aboutMenu->addAction(tr("About"));

lineAction->setCheckable (true);
rectAction->setCheckable (true);
ellipseAction->setCheckable (true);
polyAction->setCheckable (true);
brushAction->setCheckable (true);

auto figGroup = new QActionGroup(figMenu);
figGroup->addAction(lineAction);
figGroup->addAction(rectAction);
figGroup->addAction(ellipseAction);
figGroup->addAction(polyAction);
figGroup->addAction(brushAction);

//shortcuts
lineAction->setShortcut (Qt::CTRL + Qt::Key_L);
rectAction->setShortcut (Qt::CTRL + Qt::Key_R);
ellipseAction->setShortcut (Qt::CTRL + Qt::Key_E);
polyAction->setShortcut (Qt::CTRL + Qt::Key_M);
brushAction->setShortcut (Qt::CTRL + Qt::Key_B);
undoAction->setShortcut (Qt::CTRL + Qt::Key_Z);

//status tips
lineAction->setStatusTip (tr("Draw line (Ctrl + L)"));
rectAction->setStatusTip (tr("Draw rectangle (Ctrl + R)"));
ellipseAction->setStatusTip (tr("Draw ellipse (Ctrl + E)"));
polyAction->setStatusTip (tr("Draw polyline (Ctrl + M)"));
brushAction->setStatusTip (tr("Free draw (Ctrl + B)"));
undoAction->setStatusTip (tr("Undo last action (Ctrl + Z)"));
aboutAction->setStatusTip (tr("About this program"));

//tool tips
lineAction->setToolTip (tr("Draw line (Ctrl + L)"));
rectAction->setToolTip (tr("Draw rectangle (Ctrl + R)"));
ellipseAction->setToolTip (tr("Draw ellipse (Ctrl + E)"));
polyAction->setToolTip (tr("Draw polyline (Ctrl + P)"));
brushAction->setToolTip (tr("Free draw (Ctrl + B)"));
undoAction->setToolTip (tr("Undo last action (Ctrl + Z)"));

//add actions to tool bar
toolBar->addActions(figGroup->actions());
toolBar->addSeparator();
toolBar->addAction(undoAction);

//add functions to actions
//figures
connect(lineAction, &QAction::triggered, [&](bool checked) mutable {
if(checked)
_paintScene->setTool(PaintScene::Tools::line);
});
connect(rectAction, &QAction::triggered, [&](bool checked) mutable {
if(checked)
_paintScene->setTool(PaintScene::Tools::rect);
});
connect(ellipseAction, &QAction::triggered, [&](bool checked) mutable {
if(checked)
_paintScene->setTool(PaintScene::Tools::ellipse);
});
connect(polyAction, &QAction::triggered, [&](bool checked) mutable {
if(checked)
_paintScene->setTool(PaintScene::Tools::poly);
});
connect(brushAction, &QAction::triggered, [&](bool checked) mutable {
if(checked)
_paintScene->setTool(PaintScene::Tools::brush);
});

lineAction->setChecked(true);
//for shure
_paintScene->setTool(PaintScene::Tools::line);

//undo
connect(undoAction, &QAction::triggered, [&]() mutable {
_paintScene->undo();
});

connect(aboutAction, &QAction::triggered, [&](){
auto w = new QWidget(this, Qt::Window);
w->setWindowModality(Qt::WindowModal);
w->setWindowIcon(this->windowIcon());
w->setWindowTitle(tr("About"));

auto *lay = new QVBoxLayout(w);

auto image = new QLabel(w);
image->setPixmap(QPixmap(":/img/qt"));

lay->addWidget(new QLabel(tr("Made with Qt 5.7"), w), 0, Qt::AlignHCenter);
lay->addWidget(image);

w->show();
});
}


void MainWindow::saveSettings()
{
QSettings s(QCoreApplication::applicationDirPath() + "/config.ini", QSettings::IniFormat);

s.beginGroup("window");
s.setValue("geometry", this->geometry());
s.endGroup();

s.sync();
}


void MainWindow::loadSettings()
{
QSettings s(QCoreApplication::applicationDirPath() + "/config.ini", QSettings::IniFormat);

s.beginGroup("window");
this->setGeometry(s.value("geometry", QRect(100, 100, 500, 500)).toRect());
s.endGroup();
}

anda_skoa
24th October 2016, 08:35
You need to at least post one shape, otherwise it is unclear what they are needing from mainwindow.

Cheers,
_