Results 1 to 2 of 2

Thread: Reaching the information of mainwindow from a class

  1. #1
    Join Date
    Oct 2016
    Posts
    1
    Qt products
    Qt5
    Platforms
    Windows

    Question Reaching the information of mainwindow from a class

    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.

    Qt Code:
    1. #include "mainwindow.h"
    2.  
    3. MainWindow::MainWindow(QWidget *parent)
    4. : QMainWindow(parent)
    5. {
    6. this->initUi();
    7. this->loadSettings();
    8. }
    9.  
    10. MainWindow::~MainWindow()
    11. {
    12. this->saveSettings();
    13. }
    14.  
    15. void MainWindow::initUi()
    16. {
    17. this->setCentralWidget(new QWidget(this));
    18.  
    19. this->setMenuBar(new QMenuBar(this->centralWidget()));
    20.  
    21. auto toolBar = new QToolBar(this->centralWidget());
    22. this->addToolBar(toolBar);
    23.  
    24. this->setStatusBar(new QStatusBar(this->centralWidget()));
    25.  
    26. _paintScene = new PaintScene(this);
    27.  
    28. auto *layout = new QVBoxLayout(this->centralWidget());
    29.  
    30. layout->addWidget(_paintScene);
    31.  
    32. auto figMenu = this->menuBar()->addMenu(tr("Figures"));
    33. auto aboutMenu = this->menuBar()->addMenu(tr("About"));
    34.  
    35. //figures
    36. auto lineAction = figMenu->addAction(QIcon(":/img/Resources/Img/line_stroked"), tr("Draw line"));
    37. auto rectAction = figMenu->addAction(QIcon(":/img/Resources/Img/rectangle_stroked"), tr("Draw rect"));
    38. auto ellipseAction = figMenu->addAction(QIcon(":/img/Resources/Img/ellipse_stroked"), tr("Draw ellipse"));
    39. auto polyAction = figMenu->addAction(QIcon(":/img/Resources/Img/polyline"), tr("Draw polyline"));
    40. auto brushAction = figMenu->addAction(QIcon(":/img/Resources/Img/paint_brush"), tr("Free draw"));
    41. figMenu->addSeparator();
    42. auto undoAction = figMenu->addAction(QIcon(":/img/Resources/Img/undo"), tr("Undo"));
    43.  
    44. //about
    45. auto aboutAction = aboutMenu->addAction(tr("About"));
    46.  
    47. lineAction->setCheckable (true);
    48. rectAction->setCheckable (true);
    49. ellipseAction->setCheckable (true);
    50. polyAction->setCheckable (true);
    51. brushAction->setCheckable (true);
    52.  
    53. auto figGroup = new QActionGroup(figMenu);
    54. figGroup->addAction(lineAction);
    55. figGroup->addAction(rectAction);
    56. figGroup->addAction(ellipseAction);
    57. figGroup->addAction(polyAction);
    58. figGroup->addAction(brushAction);
    59.  
    60. //shortcuts
    61. lineAction->setShortcut (Qt::CTRL + Qt::Key_L);
    62. rectAction->setShortcut (Qt::CTRL + Qt::Key_R);
    63. ellipseAction->setShortcut (Qt::CTRL + Qt::Key_E);
    64. polyAction->setShortcut (Qt::CTRL + Qt::Key_M);
    65. brushAction->setShortcut (Qt::CTRL + Qt::Key_B);
    66. undoAction->setShortcut (Qt::CTRL + Qt::Key_Z);
    67.  
    68. //status tips
    69. lineAction->setStatusTip (tr("Draw line (Ctrl + L)"));
    70. rectAction->setStatusTip (tr("Draw rectangle (Ctrl + R)"));
    71. ellipseAction->setStatusTip (tr("Draw ellipse (Ctrl + E)"));
    72. polyAction->setStatusTip (tr("Draw polyline (Ctrl + M)"));
    73. brushAction->setStatusTip (tr("Free draw (Ctrl + B)"));
    74. undoAction->setStatusTip (tr("Undo last action (Ctrl + Z)"));
    75. aboutAction->setStatusTip (tr("About this program"));
    76.  
    77. //tool tips
    78. lineAction->setToolTip (tr("Draw line (Ctrl + L)"));
    79. rectAction->setToolTip (tr("Draw rectangle (Ctrl + R)"));
    80. ellipseAction->setToolTip (tr("Draw ellipse (Ctrl + E)"));
    81. polyAction->setToolTip (tr("Draw polyline (Ctrl + P)"));
    82. brushAction->setToolTip (tr("Free draw (Ctrl + B)"));
    83. undoAction->setToolTip (tr("Undo last action (Ctrl + Z)"));
    84.  
    85. //add actions to tool bar
    86. toolBar->addActions(figGroup->actions());
    87. toolBar->addSeparator();
    88. toolBar->addAction(undoAction);
    89.  
    90. //add functions to actions
    91. //figures
    92. connect(lineAction, &QAction::triggered, [&](bool checked) mutable {
    93. if(checked)
    94. _paintScene->setTool(PaintScene::Tools::line);
    95. });
    96. connect(rectAction, &QAction::triggered, [&](bool checked) mutable {
    97. if(checked)
    98. _paintScene->setTool(PaintScene::Tools::rect);
    99. });
    100. connect(ellipseAction, &QAction::triggered, [&](bool checked) mutable {
    101. if(checked)
    102. _paintScene->setTool(PaintScene::Tools::ellipse);
    103. });
    104. connect(polyAction, &QAction::triggered, [&](bool checked) mutable {
    105. if(checked)
    106. _paintScene->setTool(PaintScene::Tools::poly);
    107. });
    108. connect(brushAction, &QAction::triggered, [&](bool checked) mutable {
    109. if(checked)
    110. _paintScene->setTool(PaintScene::Tools::brush);
    111. });
    112.  
    113. lineAction->setChecked(true);
    114. //for shure
    115. _paintScene->setTool(PaintScene::Tools::line);
    116.  
    117. //undo
    118. connect(undoAction, &QAction::triggered, [&]() mutable {
    119. _paintScene->undo();
    120. });
    121.  
    122. connect(aboutAction, &QAction::triggered, [&](){
    123. auto w = new QWidget(this, Qt::Window);
    124. w->setWindowModality(Qt::WindowModal);
    125. w->setWindowIcon(this->windowIcon());
    126. w->setWindowTitle(tr("About"));
    127.  
    128. auto *lay = new QVBoxLayout(w);
    129.  
    130. auto image = new QLabel(w);
    131. image->setPixmap(QPixmap(":/img/qt"));
    132.  
    133. lay->addWidget(new QLabel(tr("Made with Qt 5.7"), w), 0, Qt::AlignHCenter);
    134. lay->addWidget(image);
    135.  
    136. w->show();
    137. });
    138. }
    139.  
    140.  
    141. void MainWindow::saveSettings()
    142. {
    143. QSettings s(QCoreApplication::applicationDirPath() + "/config.ini", QSettings::IniFormat);
    144.  
    145. s.beginGroup("window");
    146. s.setValue("geometry", this->geometry());
    147. s.endGroup();
    148.  
    149. s.sync();
    150. }
    151.  
    152.  
    153. void MainWindow::loadSettings()
    154. {
    155. QSettings s(QCoreApplication::applicationDirPath() + "/config.ini", QSettings::IniFormat);
    156.  
    157. s.beginGroup("window");
    158. this->setGeometry(s.value("geometry", QRect(100, 100, 500, 500)).toRect());
    159. s.endGroup();
    160. }
    To copy to clipboard, switch view to plain text mode 
    Attached Images Attached Images
    • File Type: png a.PNG (10.3 KB, 2 views)
    Last edited by sasafeb; 24th October 2016 at 03:51.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Reaching the information of mainwindow from a class

    You need to at least post one shape, otherwise it is unclear what they are needing from mainwindow.

    Cheers,
    _

Similar Threads

  1. Replies: 1
    Last Post: 10th March 2016, 05:04
  2. What is Type information when creating Class
    By vinothrajendran in forum Qt Tools
    Replies: 1
    Last Post: 20th March 2015, 12:11
  3. Replies: 8
    Last Post: 6th June 2013, 01:24
  4. show object (scene) in widget in class class mainwindow
    By rimie23 in forum Qt Programming
    Replies: 8
    Last Post: 1st May 2012, 16:15
  5. Replies: 4
    Last Post: 23rd August 2011, 21:53

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.