How can I return information after my main window closes in Qt?

Garett

Hi I'm new to c++ and I've been trying to create a program on Ubuntu that will take the output of Opengazer (gaze tracking software) on multiple devices and return a single coordinate. This is done by opening a UDP socket for each device which then sends a a text output of the form [Xcoordinate Ycoordinate] to the main program where the numbers will then be compared and the correct coordinate will then be output.

I found that creating a gui in Qt was the best way to establish the relative positions of each device. Basically, the problem I am having is bringing the information on the UDP sockets back through to the main program. In the getInfo function, I create the main window where my devices (another class called DeviceWidget) are created and are movable to set their relative positions. Each DeviceWidget has a UDP socket associated with it. I would like to return a QList of all of the DeviceWidgets when the window closes, but I am having a difficult time because when the window closes, all of the children are destroyed. I have also read that buttons cannot return values so this will not work either.

I am posting main.cpp and window.cpp. I can post more but I believe these are the only two necessary.

Any ideas? Thank your for your time.

Main.cpp

#include <QApplication>
#include "window.h"
#include "devicewidget.h"
#include <QTimer>

QList<DeviceWidget*> getInfo(int argc, char *argv[]);
void delay();

int main(int argc, char *argv[])
{
    bool run = true;
    int numDevices, space, size;

    DeviceWidget *point;

    QList<DeviceWidget*> dList = getInfo(argc, argv);
    numDevices = dList.size();
    int xPos[numDevices], yPos[numDevices];
    QString buffers[numDevices], xString, yString;

    //begin tracking gaze
    if(run)
    {
        for(int i=0; i<numDevices; i++)
        {
            //get output of opengazers
            point = dList.at(i);
            buffers[i] = point->Server->getBuffer();
            space = buffers[i].indexOf(" ");
            size = buffers[i].size();
            xString = buffers[i].left(space);
            yString = buffers[i].right(size-space-1);
            xPos[i] = xString.toInt();
            yPos[i] = yString.toInt();
        }
        //print coordinate
        for(int i=0; i<numDevices; i++)
        {
            if((dList.at(i)->getXRes()/6<xPos[i]<dList.at(i)->getXRes()*5/6) && (dList.at(i)->getXRes()/4<xPos[i]<dList.at(i)->getXRes()*3/4))
            {
                qDebug() << xPos[i]+dList.at(i)->getXPos()*9-dList.at(0)->getXPos()*9 << " " << yPos[i]+dList.at(i)->getYPos()*9-dList.at(0)->getYPos()*9;
            }

        }
        delay();
    }


    return 0;
}

QList<DeviceWidget*> getInfo(int argc, char *argv[])
{

    QApplication a(argc, argv);
    Window w;

    w.show();

    a.exec();

    return w.getList();

}

void delay()
{
    QTime dieTime= QTime::currentTime().addSecs(1);
    while( QTime::currentTime() < dieTime )
    QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
}

window.cpp

#include "window.h"
#include <QFrame>
#include <QPushButton>
#include <QVBoxLayout>
#include <devicewidget.h>
#include <QWidget>
#include <QDesktopWidget>
#include <QList>
#include "portdialog.h"
#include "udp.h"

Window::Window(QWidget *parent) :
    QWidget(parent),
    frame(new QFrame(this)),
    addButton(new QPushButton("Add Device", this)),
    doneButton(new QPushButton("Done", this))
{
    QVBoxLayout *layout = new QVBoxLayout;
    frame->setLineWidth(2);
    frame->setFrameStyle(QFrame::Box | QFrame::Plain);
    QPoint topLeft(0,0);
    QPoint bottomRight(100,100);
    const QRect rect(topLeft, bottomRight);
    frame->setFrameRect(rect);
    frame->setFixedHeight(300);
    frame->setFixedWidth(500);

    layout->addWidget(frame);
    layout->addWidget(addButton);
    layout->addWidget(doneButton);

    setLayout(layout);
    connect(addButton, SIGNAL(released()), this, SLOT(on_addButton_pressed()));
    connect(doneButton, SIGNAL(released()), this, SLOT(on_doneButton_pressed()));
    DeviceWidget *primary = new DeviceWidget(frame, 200, 200, 20230);
    primary->setFixedHeight(getResolutionY()/10);
    primary->setFixedWidth(getResolutionX()/10);
    primary->show();
    primary->move(200,200);
    list.append(primary);
}

void Window::on_addButton_pressed()
{
    //pop-up for port
    PortDialog *pop = new PortDialog(this);
    pop->exec();
    int port = pop->getPort();
    /*int xRes = pop->getXRes();
    int yRes = pop->getYRes();*/

    int xRes = 1360;
    int yRes = 760;

    //create and show widget
    DeviceWidget *secondary = new DeviceWidget(frame, 200, 200, port);
    secondary->createServer(port, xRes, yRes);
    secondary->setFixedHeight(secondary->getYRes() / 9);
    secondary->setFixedWidth(secondary->getXRes() / 9);
    secondary->show();
    secondary->move(200,200);
    list.append(secondary);
}

void Window::on_doneButton_pressed()
{
    this->close();
}

int Window::getResolutionX()
{
    QDesktopWidget widget;

    QRect mainScreenSize = widget.availableGeometry(widget.primaryScreen());
    return mainScreenSize.width();
}

int Window::getResolutionY()
{
    QDesktopWidget widget;

    QRect mainScreenSize = widget.availableGeometry(widget.primaryScreen());
    return mainScreenSize.height();
}

QList<DeviceWidget*> Window::getList()
{
    return list;
}
Martin

Well, that's too much code I think, next time, try to be more concise. (We don't care if your frame has a size of 300x500 or whatever :p)

I dislike what you did here:

PortDialog *pop = new PortDialog(this);
pop->exec();
int port = pop->getPort();

I think you can overcome your problem using signal and slots. In your PortDialog, you do your stuff, when you want to get a value (computed, written by user, whatever), you just emit(sigPortValue(int val));

And in your void Window::on_addButton_pressed() function, you could write

void Window::on_addButton_pressed()
{
    //pop-up for port
    PortDialog *pop = new PortDialog(this);
    connect(pop, SIGNAL(sigPortValue(int), this, SLOT(slotHandlePortValue(int));
    pop->exec();
    [...]
}

and obviously, handling the port value in the slot just mentionned above. So that's the first point.

The second point was about QList. To be clear, this QList<DeviceWidget*> getInfo(...) is a function which returns a copy of a QList. A QList of what ? Of pointers to DeviceWidget. When you copy a list, you copy the content of the list (so pointers here) but not objects pointing at. And because your DeviceWidgets has the MainWindow as a parent, they are deleted (You correclty understood that !).

I see two solutions:

orphan solution

Instead of writing DeviceWidget *primary = new DeviceWidget(frame, 200, 200, 20230);, you can omit frame, so without any parent, the DeviceWidget will not be deleted automatically. Pay attention to correctly delete the object though !

other communication way

Instead of trying to communicate information directly in the code, you can store information in a file, but it's up to you to see what's the best solution for your needs.

Hope this will help you ;)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How do I create a transparent non-modal dialog with a frame in Qt that is contained within the main application window and can contain QPushButtons

From Dev

How to add buttons to a main window in Qt?

From Dev

How can thread run after main method closes?

From Dev

How can I "add" Depth information to the main frame buffer

From Dev

How can I return information after my main window closes in Qt?

From Dev

how to set an icon on a Main window and action with QT

From Dev

How can I make my program return to the original loop after calling exec?

From Dev

How can I make the main form align correctly after my control height is autosized and then I maximize the form?

From Dev

How can I link my Junction table to my main table

From Dev

How can I return a set, array or anything of the inserted values after inserting values in a table in my postgresql function?

From Dev

How can I run my Qt function after a thread has finished?

From Dev

How can I detach a tab from my main chrome window (into a new window)?

From Dev

Main Window closes after execvp() call

From Dev

How can I return to the main menu?

From Dev

How can i return only specific information with findAll() instead of everything?

From Dev

How can I return a set, array or anything of the inserted values after inserting values in a table in my postgresql function?

From Dev

the newly opened pop up window closes itself... how can I stop this?

From Dev

How can i resize main window from slot in my class?

From Dev

main: return 0 hangs, exit 0 closes. How to debug?

From Dev

How can I return to a production build of window 10 without losing my program

From Dev

How can I make my server continue to receive messages until client closes?

From Dev

How can I detach a tab from my main chrome window (into a new window)?

From Dev

How can I save a ArrayList of custom objects after the app closes?

From Dev

How can I return my own promise?

From Dev

How can I start Hexchat minimized with the main window hidden?

From Dev

How can I display a popup window to a user after being on my PHP site for 10 minutes

From Dev

How can I increase the size of my window?

From Dev

After a CGI Python script is executed, how can I return automatically to my homepage?

From Dev

How can I change my ui object from main.cpp in qt?

Related Related

  1. 1

    How do I create a transparent non-modal dialog with a frame in Qt that is contained within the main application window and can contain QPushButtons

  2. 2

    How to add buttons to a main window in Qt?

  3. 3

    How can thread run after main method closes?

  4. 4

    How can I "add" Depth information to the main frame buffer

  5. 5

    How can I return information after my main window closes in Qt?

  6. 6

    how to set an icon on a Main window and action with QT

  7. 7

    How can I make my program return to the original loop after calling exec?

  8. 8

    How can I make the main form align correctly after my control height is autosized and then I maximize the form?

  9. 9

    How can I link my Junction table to my main table

  10. 10

    How can I return a set, array or anything of the inserted values after inserting values in a table in my postgresql function?

  11. 11

    How can I run my Qt function after a thread has finished?

  12. 12

    How can I detach a tab from my main chrome window (into a new window)?

  13. 13

    Main Window closes after execvp() call

  14. 14

    How can I return to the main menu?

  15. 15

    How can i return only specific information with findAll() instead of everything?

  16. 16

    How can I return a set, array or anything of the inserted values after inserting values in a table in my postgresql function?

  17. 17

    the newly opened pop up window closes itself... how can I stop this?

  18. 18

    How can i resize main window from slot in my class?

  19. 19

    main: return 0 hangs, exit 0 closes. How to debug?

  20. 20

    How can I return to a production build of window 10 without losing my program

  21. 21

    How can I make my server continue to receive messages until client closes?

  22. 22

    How can I detach a tab from my main chrome window (into a new window)?

  23. 23

    How can I save a ArrayList of custom objects after the app closes?

  24. 24

    How can I return my own promise?

  25. 25

    How can I start Hexchat minimized with the main window hidden?

  26. 26

    How can I display a popup window to a user after being on my PHP site for 10 minutes

  27. 27

    How can I increase the size of my window?

  28. 28

    After a CGI Python script is executed, how can I return automatically to my homepage?

  29. 29

    How can I change my ui object from main.cpp in qt?

HotTag

Archive