Hi,
I am having problem while using static variables. I want to retain screen dimensions as static variable values and use it across many classes. Hence I have following case.
Utilities class is common class having many general methods.
utilities.h
===========
#ifndef UTILITIES_H
#define UTILITIES_H
#include <QtWidgets>
class Utilities{
static int ScreenWidth;
public:
explicit Utilities();
int screenDimension(){
return ScreenWidth;
}
void setScreenDimension(int pr){
ScreenWidth = pr;
}
};
#endif // UTILITIES_H
#ifndef UTILITIES_H
#define UTILITIES_H
#include <QtWidgets>
class Utilities{
static int ScreenWidth;
public:
explicit Utilities();
int screenDimension(){
return ScreenWidth;
}
void setScreenDimension(int pr){
ScreenWidth = pr;
}
};
#endif // UTILITIES_H
To copy to clipboard, switch view to plain text mode
MainWindow class is a class which will have main window and will assign value to ScreenWidth variable of Utilities class.
mainwindow.h
============
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QtWidgets>
#include "utilities.h"
namespace Ui {
class MainWindow;
}
{
Q_OBJECT
public:
explicit MainWindow
(QWidget *parent
= 0);
~MainWindow();
protected:
private:
Ui::MainWindow *ui;
Utilities ut;
};
#endif // MAINWINDOW_H
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QtWidgets>
#include "utilities.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
protected:
void resizeEvent(QResizeEvent *e);
private:
Ui::MainWindow *ui;
Utilities ut;
};
#endif // MAINWINDOW_H
To copy to clipboard, switch view to plain text mode
mainwindow.cpp
==============
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
{
QSize graphicsViewSize
= ui
->graphicsView
->size
();
ut.setScreenDimension(graphicsViewSize.width);
}
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::resizeEvent(QResizeEvent *e)
{
QSize graphicsViewSize = ui->graphicsView->size();
ut.setScreenDimension(graphicsViewSize.width);
}
To copy to clipboard, switch view to plain text mode
When I compile this it gives me an error as below
H:\Sample\mainwindow.cpp:-1: error: undefined reference to `Utilities::ScreenWidth'
Thanks
Manish
Bookmarks