return a.exe();

第一个程序是一个简单的Hello World例子。它只包含你建立和运行Qt应用程序所需要的最少的代码。上面的图片是这个程序的快照。


 
 
  1. #include <qapplication.h> 
  2. #include <qpushbutton.h> 
  3. int main( int argc, char **argv )  
  4. {  
  5.     QApplication a( argc, argv );  
  6.     QPushButton hello( "Hello world!", 0 );  
  7.     hello.resize( 100, 30 );  
  8.     a.setMainWidget( &hello );  
  9.     hello.show();  
  10.     return a.exec();  

 
 
  1. #include <qapplication.h> 

这一行包含了QApplication类的定义。在每一个使用Qt的应用程序中都必须使用一个QApplication对象。QApplication管理了各种各样的应用程序的广泛资源,比如默认的字体和光标。


 
 
  1. #include <qpushbutton.h> 

这一行包含了QPushButton类的定义。参考文档的文件的最上部分提到了使用哪个类就必须包含哪个头文件的说明。


QPushButton是一个经典的图形用户界面按钮,用户可以按下去,也可以放开。它管理自己的观感,就像其它每一个QWidget。一个窗口部件就是一个可以处理用户输入和绘制图形的用户界面对象。程序员可以改变它的全部观感和它的许多主要的属性(比如颜色),还有这个窗口部件的内容。一个QPushButton可以显示一段文本或者一个QPixmap。


 
 
  1. int main( int argc, char **argv )  
  2.     { 

main()函数是程序的入口。几乎在使用Qt的所有情况下,main()只需要在把控制转交给Qt库之前执行一些初始化,然后Qt库通过事件来向程序告知用户的行为。

 

argc是命令行变量的数量,argv是命令行变量的数组。这是一个C/C++特征。它不是Qt专有的,无论如何Qt需要处理这些变量(请看下面)。


 
 
  1. QApplication a( argc, argv ); 

a是这个程序的QApplication。它在这里被创建并且处理这些命令行变量(比如在X窗口下的-display)。请注意,所有被Qt识别的命令行参数都会从argv中被移除(并且argc也因此而减少)。关于细节请看QApplication::argv()文档。


注意:在任何Qt的窗口系统部件被使用之前创建QApplication对象是必须的。


 
 
  1. QPushButton hello( "Hello world!", 0 ); 

这里,在QApplication之后,接着的是第一个窗口系统代码:一个按钮被创建了。


这个按钮被设置成显示“Hello world!”并且它自己构成了一个窗口(因为在构造函数指定0为它的父窗口,在这个父窗口中按钮被定位)。


 
 
  1. hello.resize( 100, 30 ); 

这个按酒被设置成100像素宽,30像素高(加上窗口系统边框)。在这种情况下,我们不用考虑按钮的位置,并且我们接受默认值。


 
 
  1. a.setMainWidget( &hello ); 

这个按钮被选为这个应用程序的主窗口部件。如果用户关闭了主窗口部件,应用程序就退出了。


你不用必须设置一个主窗口部件,但绝大多数程序都有一个。


 
 
  1. hello.show(); 

当你创建一个窗口部件的时候,它是不可见的。你必须调用show()来使它变为可见的。


 
 
  1. return a.exec(); 

这里就是main()把控制转交给Qt,并且当应用程序退出的时候exec()就会返回。


在exec()中,Qt接受并处理用户和系统的事件并且把它们传递给适当的窗口部件。


    }

你现在可以试着编译和运行这个程序了。

 

编译


编译一个C++应用程序,你需要创建一个makefile。创建一个Qt的makefile的最容易的方法是使用Qt提供的连编工具qmake。如果你已经把main.cpp保存到它自己的目录了,你所要做的就是这些:


 
 
  1. qmake -project  
  2. qmake 

第一个命令调用qmake来生成一个.pro(项目)文件。第二个命令根据这个项目文件来生成一个(系统相关的)makefile。你现在可以输入make(或者nmake,如果你使用Visual Studio),然后运行你的第一个Qt应用程序!


行为


当你运行它的时候,你就会看到一个被单一按钮充满的小窗口,在它上面你可以读到著名的词:Hellow World!

  • 10
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
#coding=utf-8 import json import urllib.request import time import traceback #模拟成浏览器 headers={"Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", "Accept-Encoding":"gbk,utf-8,gb2312", "Accept-Language":"zh-CN,zh;q=0.8", "User-Agent":"Mozilla/5.0(Windows NT 10.0; WOW64) AppleWebKit/537.36(KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36", "Connection":"keep-alive"} opener=urllib.request.build_opener() headall=[] for key,value in headers.items(): item=(key,value) headall.append(item) opener.addheaders=headall #将opener安装为全局 urllib.request.install_opener(opener) #构造数据,城市分别是北京、天津、石家庄、太原、济南、沈阳、呼和浩特、郑州 city_id = ['54511','54517','53698','53772','54823','54342','53463','57083'] def getcityid(city): if city == 'beijing': return city_id[0] elif city == 'tianjin': return city_id[1] elif city == 'shijiazhuang': return city_id[2] elif city == 'taiyuan': return city_id[3] elif city == 'jinan': return city_id[4] elif city == 'shenyang': return city_id[5] elif city == 'huhehaote': return city_id[6] else: return city_id[7] def getweather(city): try: url = "http://www.nmc.cn/f/rest/real/"+getcityid(city) stdout = urllib.request.urlopen(url) weatherInfo = stdout.read().decode('utf-8') jsonData = json.loads(weatherInfo) weatherlist = [] # 读取JSON数据,添加到列表中 szDate = jsonData["publish_time"] weatherlist.append(szDate) szCity = jsonData["station"]["city"] print("城市: "+str(szCity)) szWeather = jsonData["weather"]["info"] weatherlist.append(szWeather) szdirect = jsonData["wind"]["direct"] weatherlist.append(szdirect) szspeed = str(jsonData["wind"]["speed"]) + "m/s" weatherlist.append(szspeed) szTemp = str(jsonData["weather"]["temperature"]) + "℃" weatherlist.append(szTemp) szhumidity = str(int(jsonData["weather"]["humidity"])) + "%" weatherlist.append(szhumidity) print("数据更新时间,天气,风向,风速,实时温度,相对湿度:") print(weatherlist) writefiles_weather(city,weatherlist) except urllib.error.URLError as e: print("获取天气状况数据出现URLERROR!一分钟后重试……") if hasattr(e, "code"): print(e.code) if hasattr(e, "reason"): print(e.reason) time.sleep(60) # 出现异常则过一段时间重新执行此部分 getweather(city) except Exception as e: print("获取天气状况数据出现EXCEPTION!十秒钟后重试……") print("Exception:" + str(e)) traceback.print_exc() # 获得错误行数 time.sleep(10) # 出现异常则过一段时间重新执行此部分 getweather(city) def writefiles_weather(filename,weatherlist): try: #将获取的数据写入文件中,数据分别为数据更新时间,天气,风向,风速(m/s),实时温度(℃),相对湿度(%)。 with open("weather"+filename+".txt","a",errors="ignore") as f: for weather in weatherlist: f.write(str(weather)) f.write(",") f.write("\n") print("该条天气数据已添加到文件中!") except Exception as e: print("天气状况数据写入文件函数出现异常!将跳过此部分……") print("Exception:"+str(e)) traceback.print_exc() #获得错误行数 pass if __name__ == '__main__': while(True): print("==========开始工作==========") getweather("beijing") getweather("tianjin") getweather("shijiazhuang") getweather("taiyuan") getweather("jinan") getweather("shenyang") getweather("huhehaote") getweather("zhengzhou") #休息一小时 print("【休息中……】") time.sleep(3600)
在b.exe运行结束时,可以通过将结果写入共享内存或文件的方式来实现返回string类型的结果。具体步骤如下: 1. 在a.exe中创建一个共享内存或文件,用于存储b.exe返回的结果。 2. 在a.exe中启动b.exe进程,并将共享内存地址或文件路径作为参数传递给b.exe。 3. 在b.exe中将结果写入共享内存或文件中。 4. 在a.exe中读取共享内存中的结果或读取文件中的结果,并将其转换为string类型。 下面是一个示例代码: 在a.exe中: ```c++ #include <windows.h> #include <iostream> int main() { HANDLE hMapFile = CreateFileMapping( INVALID_HANDLE_VALUE, // use paging file NULL, // default security PAGE_READWRITE, // read/write access 0, // maximum object size (high-order DWORD) sizeof(char[256]), // maximum object size (low-order DWORD) "Local\\MyFileMappingObject"); // name of the mapping object if (hMapFile == NULL) { std::cout << "Could not create file mapping object: " << GetLastError() << std::endl; return 1; } char* pBuffer = (char*)MapViewOfFile( hMapFile, // handle to mapping object FILE_MAP_ALL_ACCESS, // read/write permission 0, // high-order DWORD of offset 0, // low-order DWORD of offset 256); // number of bytes to map if (pBuffer == NULL) { std::cout << "Could not map view of file: " << GetLastError() << std::endl; CloseHandle(hMapFile); return 1; } STARTUPINFO si = { 0 }; si.cb = sizeof(si); PROCESS_INFORMATION pi = { 0 }; TCHAR szPath[MAX_PATH]; GetModuleFileName(NULL, szPath, MAX_PATH); if (!CreateProcess(szPath, (LPSTR)"b.exe Local\\MyFileMappingObject", NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) { std::cout << "CreateProcess failed: " << GetLastError() << std::endl; UnmapViewOfFile(pBuffer); CloseHandle(hMapFile); return 1; } WaitForSingleObject(pi.hProcess, INFINITE); std::cout << "Result from b.exe: " << pBuffer << std::endl; UnmapViewOfFile(pBuffer); CloseHandle(hMapFile); return 0; } ``` 在b.exe中: ```c++ #include <windows.h> #include <iostream> int main(int argc, char* argv[]) { HANDLE hMapFile = OpenFileMapping( FILE_MAP_ALL_ACCESS, // read/write access FALSE, // do not inherit the name argv[1]); // name of the mapping object if (hMapFile == NULL) { std::cout << "Could not open file mapping object: " << GetLastError() << std::endl; return 1; } char* pBuffer = (char*)MapViewOfFile( hMapFile, // handle to mapping object FILE_MAP_WRITE, // write permission 0, // high-order DWORD of offset 0, // low-order DWORD of offset 256); // number of bytes to map if (pBuffer == NULL) { std::cout << "Could not map view of file: " << GetLastError() << std::endl; CloseHandle(hMapFile); return 1; } strcpy_s(pBuffer, 256, "Hello, World!"); UnmapViewOfFile(pBuffer); CloseHandle(hMapFile); return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值