简体   繁体   English

如何从GUI编写结构

[英]How to write to struct from a GUI

Using Qt, I'm trying to write to my struct via inputs from a gui. 使用Qt,我试图通过gui的输入写入我的结构。

my target.h file: 我的target.h文件:

struct Target{
    double heading;
    double speed;
};

my cpp: 我的cpp:

#include <target.h>

struct Target myship;

myship.heading = 0;
myship.speed = 0;

I am using a QDial for the heading as an example. 我以标题的QDial为例。 I can write the value of the QDial to a text file, but I would like to take advantage of structs. 我可以将QDial的值写入文本文件,但是我想利用结构。

What I'd like to know is how do I access, so that I can write to, the struct in my mainwindow.cpp? 我想知道的是如何访问mainwindow.cpp中的结构,以便可以对其进行写入?

I see that I can access my Target structure in mainwindow.cpp like this: 我看到我可以这样在mainwindow.cpp中访问我的Target结构:

Target.heading

But it won't find "myship". 但是,它不会找到“ myship”。 I would have thought that I could have done 我本以为可以做到

myship.heading...

or 要么

Target.myship.heading...

But neither is working. 但是都没有用。 When I do Target.heading it gives me the error 当我执行Target.heading时,它给了我错误

expected unqualified-id before '.' token

My ultimate goal is to have my gui (QDial in this case) write to the struct, and then have my gui (QLabel) display what has been written. 我的最终目标是让我的gui(在这种情况下为QDial)写入该结构,然后让我的gui(QLabel)显示所编写的内容。 As mentioned before, I have the read/write working with a text file, but I'm currently only writing out a single value, which isn't going to meet my requirements. 如前所述,我可以对文本文件进行读/写操作,但是我目前仅写出一个值,无法满足我的要求。

I'm new to Qt and structs in general so my guess is I'm missing something pretty trivial, or my understanding is off completely. 我是Qt和struct的新手,所以我的猜测是我缺少一些琐碎的东西,或者我的理解完全不对。

The struct prefix that you've used in the definition of the myship variable is a C-ism. myship变量的定义中使用的struct前缀是C-ism。 It doesn't belong in C++. 它不属于C ++。 You should define myship as: 您应该将myship定义为:

Target myship;

Furthermore, since it's 2016, you should use everything C++11 has got to make your life easier. 此外,自2016年以来,您应该使用C ++ 11所需要的一切来简化您的生活。 Initialization of non-static/non-const class/struct members is very helpful and avoids boilerplate at the point of use of the struct. 非静态/非const类/结构成员的初始化非常有帮助,并且在使用结构时避免了样板。 Thus, prefer: 因此,更喜欢:

// target.h
#include <QtCore>
struct Target {
  double heading = 0.0;
  double speed = 0.0;
};
QDebug operator(QDebug dbg, const Target & target);

// target.cpp
#include "target.h"
QDebug operator(QDebug dbg, const Target & target) {
  return dbg << target.heading << target.speed;
}

// main.cpp
#include "target.h"
#include <QtCore>

int main() {
  Target ship;
  qDebug() << ship;
}

Note that you should include your own headers as #include "header.h" , not #include <header.h> . 请注意,您应该将自己的标头包含在#include "header.h" ,而不是#include <header.h> The latter is reserved for system headers. 后者是为系统头保留的。

Without Qt : 没有Qt

#include <iostream>
struct Target {
  double heading = 0.0;
  double speed = 0.0;
};

int main() {
  Target ship;
  std::cout << ship.heading << " " << ship.speed << std::endl;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM