I think my problem is very simple and I'm probably overlooking something incredibly obvious, but I haven't been able to solve this for a little while now.
In my current QT project, I have a 2 files, one of them is a window, another is a widget. My window calls upon my "custom" widget and creates an instance of it and injects it into the frame. This placing of the widget into the window is controlled by a button which can be clicked many times because I ultimately want many instances of the widget on the screen at once.
Here's the problem: Whenever a new instance of my widget is placed in the frame, they seem to just be mirroring each others values, meaning when I do stuff within the first instance of the widget, it happens in every instance of the widget (however, it sometimes doesn't appear to update right away, but I know that it happens). I want them to be separate instances which will in the future interact with each other.
Here's how I'm handling injecting the widget:
void Window::on_btnNewCell_clicked()
{
ui->verLay->addWidget(new Neuron());
}
And here's the main portion of my widget's code:
#include "neuron.h"
#include "ui_neuron.h"
#include "qtimer.h"
// Define Neuron Properties
int volt = -70;
int rest = -70;
int max = 40;
// Logic Contol
int excAmt = 10;
int inhAmt = -10;
bool refract = false;
bool timerActive;
bool st = true;
Neuron::Neuron(QWidget *parent):QWidget(parent), ui(new Ui::Neuron)
{
ui->setupUi(this);
//Homeostatic Voltage Changing Timer
tiHom = new QTimer(this);
connect(tiHom, SIGNAL(timeout()), this, SLOT(changeVoltage()),Qt::DirectConnection );
//Duration of mouse depression timer
tiPrs = new QTimer(this);
connect(tiPrs, SIGNAL(timeout()), this, SLOT(chgVltg()),Qt::DirectConnection );
timerActive = false;
}
Neuron::~Neuron()
{
delete ui;
}
// CUSTOM METHODS
void Neuron::changeVoltage(int c)
{
volt = (volt + c);
if (volt >= max) // begin action potential
{
volt = rest;
timerActive = false;
tiHom->stop();
ui->chkFired->setChecked(true);
}
if (volt <= rest) // to not drop below -40
{
if (timerActive == true)
{
timerActive = false;
tiHom->stop();
}
volt = rest;
}
else if (volt >= rest) // to drop it to -40
{
if (timerActive == false)
{
timerActive = true;
tiHom->start(100);
}
}
ui->lblVolt->setText(QString::number(volt));
}
void Neuron::changeVoltage()
{
changeVoltage(-1);
}
void Neuron::chgVltg()
{
if (st == true)
{
changeVoltage(excAmt);
}
else
changeVoltage(inhAmt);
}
void Neuron::updateGlow(int i, bool b)
{
QPixmap pic;
if (b == true) {
if (st == true) pic.load(":/Images/Excitatory/" +QString::number(i) +".png");
else pic.load(":/Images/Inhibitory/" +QString::number(i) +".png");
ui->lblE->setPixmap(pic);
ui->lblE->setEnabled(true);
tiPrs->start(100);
}
else
{
ui->lblE->setEnabled(false);
tiPrs->stop();
}
}
//many additional redundant methods for a lack of an efficient way of implementing them
'volt' isn't a member variable - it's a file local variable. There's only one of them. Make it a member variable (put it in the class), and your problem will vanish.
Something like:
class Neuron {
private:
int volt;
};
And in the constructor, set it's initial value:
Neuron::Neuron(QWidget *parent) : QWidget(parent), ui(new Ui::Neuron), volt(5)
{ /* existing code */ }