Rob
Rob

Reputation: 169

Proper way to Access Mainwindows Private variables from another class

I am working on a program where I create several stacked windows to work in with different functions. These windows are made in their own separated classes. One of the stacked windows holds several docks and I want to let them be toggle-able from the windows menu to show or hide them. I can do this if they are part of the mainwindow class but then the file is huge. My problem is that I can not seem to be able to get the layout to even show up when I make the editor class inherit the main window because the main window already has a layout. And even then the compiler was still throwing an error for the line that adds the action to the menu. Does anybody have any suggestions for how I can keep these in separate classes but still be able to use the private members of the mainwindow class. Thanks for the help in advanced!

Part of main window where the editor page is called

MainWindow::MainWindow(QWidget *parent)
{
createActions();
createMenu();
createStatusBar();
createDocks();

createMainWidget();

readSettings();

setWindowTitle("Black Ops Bsuiness Group's ERP System - Version 0.0.0.1");
}

void MainWindow::createMainWidget()
{
contentsIconWidget = new QListWidget();
contentsIconWidget->setViewMode(QListView::IconMode);
contentsIconWidget->setIconSize(QSize(70,70));
contentsIconWidget->setMovement(QListView::Static);
contentsIconWidget->setMaximumWidth(75);
contentsIconWidget->setMinimumWidth(75);
contentsIconWidget->setSpacing(10);
contentsIconWidget->setStyleSheet("QListWidget {background-color:#333;    color:#0099FF;}");

mainContentPages = new QStackedWidget();
mainContentPages->addWidget(new HomePage);
mainContentPages->addWidget(new AnalyticsPage);
mainContentPages->addWidget(new EditorPage);
mainContentPages->setStyleSheet("QStackedWidget {border:1px solid #888;}");
setCentralWidget(mainContentPages);

createIcons();
contentsIconWidget->setCurrentRow(0);

QHBoxLayout *hMainLayout = new QHBoxLayout();
hMainLayout->addWidget(contentsIconWidget,0);
hMainLayout->addWidget(mainContentPages,0);
hMainLayout->setSpacing(0);
hMainLayout->setMargin(0);

QFrame *mFrame = new QFrame(this);
mFrame->setLayout(hMainLayout);
setCentralWidget(mFrame);
}

Part of views source page where the different view states layouts are stored

EditorPage::EditorPage(QWidget *parent) : QWidget(parent)
{
mdiArea = new QMdiArea;
mdiToolBox = new QToolBox;

QDockWidget *dock = new QDockWidget(tr("SQL Manager"));
//MainWindow::windowMenu->addAction(dock->toggleViewAction());
//MainWindow::windowMenu->addAction(dock->toggleViewAction());

QDockWidget *dock1 = new QDockWidget(tr("SQL Manager"));

QSplitter *splitter = new QSplitter();
splitter->setOrientation(Qt::Vertical);
splitter->addWidget(mdiArea);
splitter->addWidget(dock);

QSplitter *splitter1 = new QSplitter();
splitter1->setOrientation(Qt::Horizontal);
splitter1->addWidget(dock1);
splitter1->addWidget(splitter);

QHBoxLayout *mainLayout = new QHBoxLayout();
mainLayout->addWidget(splitter1);
mainLayout->setSpacing(0);
mainLayout->setMargin(0);
setLayout(mainLayout);

}

Part of header file for view classes

class EditorPage : public QWidget
{
public:
EditorPage(QWidget *parent = 0);

private:
QMdiArea *mdiArea;
QToolBox *mdiToolBox;
};

Upvotes: 0

Views: 498

Answers (1)

Mat
Mat

Reputation: 206669

The Qt way is to use signals and slots for this.

Your editor window would get a toggleDock() slot (possibly taking an int or an enum value), and the main window would invoke that slot via signals (either a custom signal, something triggered by a QAction, or from a button widget).

With this, there is no static dependency between your classes.

(Note that storing the pointers to your dock widgets as members (possibly in an array) will likely make this easier to code.)

Upvotes: 1

Related Questions