我怎么能返回信息后,我的主窗口在Qt关闭
How can I return information after my main window closes in Qt?
嗨,我是c++的新手,我一直在尝试在Ubuntu上创建一个程序,该程序将在多个设备上输出Opengazer(凝视跟踪软件)并返回单个坐标。这是通过为每个设备打开一个UDP套接字来完成的,然后将[Xcoordinate Ycoordinate]形式的文本输出发送到主程序,然后将数字进行比较,然后输出正确的坐标。
我发现在Qt中创建gui是建立每个设备相对位置的最佳方式。基本上,我遇到的问题是将UDP套接字上的信息带回主程序。在getInfo函数中,我创建了主窗口,其中创建了设备(另一个名为DeviceWidget的类),并且可以移动以设置它们的相对位置。每个DeviceWidget都有一个与之关联的UDP套接字。当窗口关闭时,我想返回所有DeviceWidgets的QList,但是我有一个困难的时间,因为当窗口关闭时,所有的孩子都被破坏了。我也读到按钮不能返回值,所以这也行不通。
我张贴main.cpp和window.cpp。我可以发表更多,但我相信只有这两个是必要的。
任何想法?谢谢你的宝贵时间。
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;
}
我觉得代码太多了,下次尽量简洁一点。(我们不关心你的框架是300x500还是其他大小:p)
我不喜欢你在这里所做的。
PortDialog *pop = new PortDialog(this);
pop->exec();
int port = pop->getPort();
我认为你可以用信号和插槽来解决你的问题。在你的PortDialog中,你做你的东西,当你想要得到一个值(计算的,由用户写的,无论什么),你只要emit(sigPortValue(int val));
在void Window::on_addButton_pressed()
函数中,你可以写
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();
[...]
}
,显然,处理上面提到的槽中的端口值。这是第一点
第二点是关于QList。需要明确的是,这个QList<DeviceWidget*> getInfo(...)
是一个返回QList副本的函数。什么清单?指向DeviceWidget的指针。复制列表时,复制的是列表的内容(这里是指针),而不是指向的对象。因为你的DeviceWidgets有主窗口作为父窗口,所以它们被删除了(你正确地理解了这一点!)。
我看到两个解决方案:
<<p> 孤儿解决方案/strong>不写DeviceWidget *primary = new DeviceWidget(frame, 200, 200, 20230);
,您可以省略frame
,因此没有任何父节点,DeviceWidget将不会被自动删除。但是要注意正确地delete
对象!
其他通信方式
您可以将信息存储在文件中,而不是尝试在代码中直接交流信息,但您可以根据需要查看最佳解决方案。
希望对你有帮助;)
如果您的窗口可以从QDialog派生,那么您可能能够使用QDialog::exec()
来本地控制事件循环。
如果我正确地理解了你的问题,下面似乎是在做一些你所问的事情:它显示了一个QDialog
和一个QTextEdit
,你可以输入文本,一旦你关闭那个窗口,主函数就继续,显示一个QDialog
,从QTextEdit
获取信息并显示它。
#include <QApplication>
#include <QDialog>
#include <QGridLayout>
#include <QTextEdit>
#include <QPushButton>
#include <QLabel>
int main(int argc, char** argv)
{
QApplication app(argc,argv);
// show a dialog that lets you enter text
QDialog dialog;
QGridLayout layout(&dialog);
QTextEdit edit;
layout.addWidget(&edit);
// this will block until the window is closed
dialog.exec();
// open a new dialog....
QDialog closeMe;
QGridLayout layout2(&closeMe);
QLabel label;
// ... retrieve the text from the dialog we created first
label.setText(QObject::tr("You entered: ")+edit.toPlainText());
QPushButton button("Click here to close application");
layout2.addWidget(&label,0,0);
layout2.addWidget(&button,1,0);
QObject::connect( &button, SIGNAL( clicked() ), &closeMe, SLOT( accept() ) );
closeMe.show();
return app.exec();
}
- 如何在Qt窗口小部件中使用QStringView(或QStringRef)
- 如何在cpp文件之间切换窗口?在Qt中
- 如果不在窗口 10 中声明名为 openCV 的 openCV namedWindow,QT 图像显示将无法正常工作
- 关于如何使用指向主窗口的指针的 QT 问题
- 如何等待窗口隐藏在Qt中?
- Qt:从另一个窗口访问公共对象
- 如何在Qt中连接来自不同窗口的两个对象?
- 如何使用Qt自动捕获窗口
- 在Visual Studio中单实例Qt应用程序版本5.11.1中,在所有其他窗口的顶部打开Qt MainWindow
- 如何使用Qt在新窗口中显示视频帧
- Qt5 C++将新窗口与Qt设计器链接
- QT 如何将变量传递到主窗口
- 使用cmake(窗口)构建时不显示Qt资源
- 在qt窗口中具有图像,而无需将图像文件本身作为源
- 没有互联网弹出窗口Qt和QML(移动设备)
- 信号和插槽之间的2个窗口QT
- 从窗口 QT 打开新窗口
- 打开窗口qt时应用程序崩溃
- 传递矢量对象到新的对话窗口(Qt c++)
- 我该如何将我的长函数移出主窗口(QT)