No, I cannot access to the tool object:
mymacros.h:
#ifndef MYMACROS_H
#define MYMACROS_H
#define WALL 3
#define ENTRY 2
#define EXIT 1
#define REMOVE 0
#endif // MYMACROS_H
#ifndef MYMACROS_H
#define MYMACROS_H
#define WALL 3
#define ENTRY 2
#define EXIT 1
#define REMOVE 0
#endif // MYMACROS_H
To copy to clipboard, switch view to plain text mode
tool.h:
#ifndef TOOL_H
#define TOOL_H
#include <QObject>
{
Q_OBJECT
int Type;
public slots:
void SetWall();
void SetEntry();
void SetExit();
void SetRemove();
};
#endif // TOOL_H
#ifndef TOOL_H
#define TOOL_H
#include <QObject>
class Tool : public QObject
{
Q_OBJECT
int Type;
public slots:
void SetWall();
void SetEntry();
void SetExit();
void SetRemove();
};
#endif // TOOL_H
To copy to clipboard, switch view to plain text mode
tool.cpp:
#include "tool.h"
#include "mymacros.h"
void Tool:: SetWall()
{
Type = WALL;
}
void Tool :: SetEntry()
{
Type = ENTRY;
}
void Tool::SetExit()
{
Type = EXIT;
}
void Tool::SetRemove()
{
Type = REMOVE;
}
#include "tool.h"
#include "mymacros.h"
void Tool:: SetWall()
{
Type = WALL;
}
void Tool :: SetEntry()
{
Type = ENTRY;
}
void Tool::SetExit()
{
Type = EXIT;
}
void Tool::SetRemove()
{
Type = REMOVE;
}
To copy to clipboard, switch view to plain text mode
toolbar.h:
#ifndef TOOLBAR_H
#define TOOLBAR_H
#include <mainwindow.h>
void CreateToolBar(MainWindow* win, Tool *tool);
#endif // TOOLBAR_H
#ifndef TOOLBAR_H
#define TOOLBAR_H
#include <mainwindow.h>
void CreateToolBar(MainWindow* win, Tool *tool);
#endif // TOOLBAR_H
To copy to clipboard, switch view to plain text mode
toolbar.cpp:
#include "toolbar.h"
#include "ui_mainwindow.h"
#include "tool.h"
void CreateToolBar(MainWindow *win, Tool *tool)
{
ToolBar->insertAction(EntryButton,WallButton);
ToolBar->insertAction(ExitButton,EntryButton);
ToolBar->insertAction(RemoveButton,ExitButton);
ToolBar->insertAction(0,RemoveButton);
win->addToolBar(ToolBar);
// here I connect the buttons of the toolbar with the tool SLOTs (which change its status)
win->connect(WallButton,SIGNAL(triggered()),tool, SLOT(SetWall()));
win->connect(RemoveButton,SIGNAL(triggered()),tool, SLOT(SetRemove()));
win->connect(ExitButton,SIGNAL(triggered()),tool, SLOT(SetExit()));
win->connect(EntryButton,SIGNAL(triggered()),tool, SLOT(SetEntry()));
}
#include "toolbar.h"
#include "ui_mainwindow.h"
#include "tool.h"
void CreateToolBar(MainWindow *win, Tool *tool)
{
QToolBar *ToolBar = new QToolBar("ToolBar");
QAction *WallButton=new QAction(QIcon(QApplication::applicationDirPath().append("/img/Wall.bmp")),"Wall",win);
QAction *EntryButton=new QAction(QIcon(QApplication::applicationDirPath().append("/img/Entry.bmp")),"Entry",win);
QAction *ExitButton=new QAction(QIcon(QApplication::applicationDirPath().append("/img/Exit.bmp")),"Exit",win);
QAction *RemoveButton=new QAction(QIcon(QApplication::applicationDirPath().append("/img/Remove.bmp")),"Remove",win);
ToolBar->insertAction(EntryButton,WallButton);
ToolBar->insertAction(ExitButton,EntryButton);
ToolBar->insertAction(RemoveButton,ExitButton);
ToolBar->insertAction(0,RemoveButton);
win->addToolBar(ToolBar);
// here I connect the buttons of the toolbar with the tool SLOTs (which change its status)
win->connect(WallButton,SIGNAL(triggered()),tool, SLOT(SetWall()));
win->connect(RemoveButton,SIGNAL(triggered()),tool, SLOT(SetRemove()));
win->connect(ExitButton,SIGNAL(triggered()),tool, SLOT(SetExit()));
win->connect(EntryButton,SIGNAL(triggered()),tool, SLOT(SetEntry()));
}
To copy to clipboard, switch view to plain text mode
so when the user chooses a button on the toolbar the status is stored in the integer member of Tool.
moreover in the mainwindow constructor I create a tool object which is passed to CreateToolbar to associate the toolbar buttons to the tool SLOTs:
part of the main.cpp:
//...
Tool *tool = new Tool;
MainWindow w(tool);
//...
//...
Tool *tool = new Tool;
MainWindow w(tool);
//...
To copy to clipboard, switch view to plain text mode
mainwindowcpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "toolbar.h"
#include "tool.h"
//MainWindow constructor
MainWindow
::MainWindow(Tool
*tool,
QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
CreateToolBar(this, tool);
}
MainWindow::~MainWindow()
{
delete ui;
}
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "toolbar.h"
#include "tool.h"
//MainWindow constructor
MainWindow::MainWindow(Tool *tool, QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
CreateToolBar(this, tool);
}
MainWindow::~MainWindow()
{
delete ui;
}
To copy to clipboard, switch view to plain text mode
so the tool is declared in the main and is accessible only in the MainWindow constructor, not in the ButtonMatrix object (which is declared in the main)
Bookmarks