zer02pi
Posts: 18
Joined: Thu Dec 01, 2016 6:39 pm

calling integer from sub window

Mon Dec 05, 2016 11:23 pm

i have two windows in this program. sub window (pinsettings) for just defining pins with gui. i can save these pin numbers in integers(in rp, yp, gp). but i can not call them in mainwindow. how can i use global variables in c++? btw i tried to google and find "extern int" i have added extern int top of the mainwindow.h and tried, it didn't work either.

mainwindow.h

Code: Select all

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include "pinsettings.h"

extern int rp, yp, gp;

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private slots:
    void on_actionPIN_Settings_triggered();

    void on_start_clicked();

    void runLoop();

private:
    Ui::MainWindow *ui;
    pinsettings *pinset;
};

#endif // MAINWINDOW_H
pinsettings.h

Code: Select all

#ifndef PINSETTINGS_H
#define PINSETTINGS_H

#include <QWidget>

extern int rp, yp, gp;

namespace Ui {
class pinsettings;
}

class pinsettings : public QWidget
{
    Q_OBJECT

public:
    explicit pinsettings(QWidget *parent = 0);
    ~pinsettings();

private slots:
    void on_butAccept_clicked();

private:
    Ui::pinsettings *ui;
};

#endif // PINSETTINGS_H
main.cpp

Code: Select all

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}
mainwindow.cpp

Code: Select all

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "wiringPi.h"
#include "qmessagebox.h"

int rt2, yt2, gt2;

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    setenv("WIRINGPI_GPIOMEM", "1", 1);
    wiringPiSetupGpio();

    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_actionPIN_Settings_triggered()
{
    pinset = new pinsettings(this);
    pinset->show();
}

void MainWindow::on_start_clicked()
{
    int rt = ui->rtime->text().toInt();
    int yt = ui->ytime->text().toInt();
    int gt = ui->gtime->text().toInt();

    if (!(rt && yt && gt))
        QMessageBox::information(this, "Error!", "You have to input time values!");

    rt2 = rt;
    yt2 = yt;
    gt2 = gt;

//    pinMode(rp, OUTPUT);
//    pinMode(yp, OUTPUT);
    pinMode(gp, OUTPUT);
}
pisettings.cpp

Code: Select all

#include "pinsettings.h"
#include "ui_pinsettings.h"
#include "qmessagebox.h"

pinsettings::pinsettings(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::pinsettings)
{
    ui->setupUi(this);
}

pinsettings::~pinsettings()
{
    delete ui;
}

public static int rp;

void pinsettings::on_butAccept_clicked()
{
    rp = ui->rpin->text().toInt();
    yp = ui->ypin->text().toInt();
    gp = ui->gpin->text().toInt();

    if (rp && yp && gp)
    {
        if (rp == gp || rp == yp || gp == yp)
            QMessageBox::information(this, "Error!", "You have defined same PIN number for different LEDs!");
        else
            close();
    }
    else
        QMessageBox::information(this, "Error!", "You have to define PIN numbers!");
}
i have one more question.
how can i stop infinite loop? how can i send interrupt?

User avatar
topguy
Posts: 4616
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: calling integer from sub window

Tue Dec 06, 2016 2:25 pm

This looks like the perfect time to learn about Qt Signal and Slots.
http://doc.qt.io/qt-5/signalsandslots.html

You could probably solve your problem with an "extern" but it just would be bad practice and result in ugly code.

zer02pi
Posts: 18
Joined: Thu Dec 01, 2016 6:39 pm

Re: calling integer from sub window

Tue Dec 06, 2016 7:54 pm

topguy wrote:This looks like the perfect time to learn about Qt Signal and Slots.
http://doc.qt.io/qt-5/signalsandslots.html

You could probably solve your problem with an "extern" but it just would be bad practice and result in ugly code.
i looked signals and slots wiki page and some tutorial. tutorial just shows slider which is connected to progress bar. they are same window also. i could not find tutorial about calling data from another window.

according to wiki page. i tried to create public signal (signal's tag "lineEdit") in sub window. (let assume, i want to call data in sub window to main window. and sub window's pointer is "sw") and in main window cpp page i tried this:

sw->lineEdit();

when i do this it is not calling data i think. it tries to call a function again. and sw->lineEdit()->text(); not works either.
normally, i can call data with this (ui->lineEdit->text()). but i can not use this switching pointer.

shortly can you show me how i can do this, if it does not take lot of time for you. if it does, no problem thank you anyway. :)

User avatar
mad-hatter
Posts: 419
Joined: Sun Feb 26, 2012 2:58 pm
Location: By the UK seaside

Re: calling integer from sub window

Thu Dec 08, 2016 7:23 pm

Hello,

This shows signals and slots between a qmainwindow and a qdialog.
Similar principles will apply between two qdialogs.

http://www.qtcentre.org/threads/66953-Q ... post294164

Regards

Lineaxe
Posts: 21
Joined: Fri Nov 06, 2015 1:16 pm

Re: calling integer from sub window

Sun Dec 11, 2016 1:43 am

you can define a variable in the sub window header .. Usually you create and then use public get and set methods to access that variable in order to protect it. This works with all kinds of stuff besides integers.

Then in your mainwindow you can do something like the following pseudocode :
subwindow s;
s.setVar1(32);
int x = s.getVar1();
----- also you can pass in values into the constructor ,if you just need to initialize things
Signals and Slots are a great way to go. There is also a Youtube video on using Signals and slots. It is part of a series of 100+ videos on using the QT environment.

Return to “C/C++”

Who is online

Users browsing this forum: No registered users and 5 guests