show()和exec() (zz)

本文探讨了QDialog中show()与exec()函数的区别。show()用于显示非模式对话框,控制权立即返回调用方;而exec()则用于显示模式对话框,会锁定程序直至用户关闭对话框。此外,文章还提供了如何在QWidget中模拟模式对话框的方法。

http://hi.baidu.com/wangjuns8/blog/item/24b382460dd1c1338694737d.html/cmtid/2fb3818c9b4f1ef5503d9202

QDialog的显示有两个函数show()和exec()。他们的区别在参考文档上的解释如下:

show():
显示一个非模式对话框。控制权即刻返回给调用函数。
弹出窗口是否模式对话框,取决于modal属性的值。
(原文:Shows the dialog as a modeless dialog. Control returns immediately to the calling code. 
The dialog will be modal or modeless according to the value of the modal property. )

exec():
显示一个模式对话框,并且锁住程序直到用户关闭该对话框为止。函数返回一个DialogCode结果。
在对话框弹出期间,用户不可以切换同程序下的其它窗口,直到该对话框被关闭。
(原文:Shows the dialog as a modal dialog, blocking until the user closes it. The function returns a DialogCode result. 
Users cannot interact with any other window in the same application until they close the dialog. )

先简单说一下我对模式和非模式对话框的一点点理解:
模式对话框,就是在弹出窗口的时候,整个程序就被锁定了,处于等待状态,直到对话框被关闭。这时往往是需要对话框的返回值进行下面的操作。如:确认窗口(选择“是”或“否”)。
非模式对话框,在调用弹出窗口之后,调用即刻返回,继续下面的操作。这里只是一个调用指令的发出,不等待也不做任何处理。如:查找框。

从字面上看,show()即可以显示非模式也可以显示模式对话框(设置modal值)。当modal=true的时候是否跟exec()就一样了呢?
经过测试,还是有区别的。
使用show(),虽然在对话框弹出的时候,程序的其它操作(按钮、窗口切换等)都失效了;但是程序仍然在调用对话框之后,马上返回继续执行后面的代码。这样,你就不会得到窗口的返回值。以这个来看,show()只能算是“半模式”。
而使用exec(),在调用之后,程序就被锁定在原地。等待窗口的关闭。

实际上,QDialog的show()函数来自其父类QWidget。而exec()则是自己的。

我最近特别喜欢继承QWidget类来做弹出窗口,它的好处就是方便、灵活,既可以做为弹出窗口用也可以嵌入另外的页面里(QDialog是不行的)。但问题是,QWidget没有exec()函数。所以想弹出这样模拟出来的模式对话框就不行了。
也查过一些资料,有说用while(true)循环的,有说用接收事件的,但都感觉不太好。

所以,目前还没有找到比较好的解决办法。

续:
上面遗留的问题经过查看QTE源代码(没有找到QT的)总算解决了。
我查看了QDialog类的exec()函数。发现里面同样调用了show(),只是在后面又调用了一句qApp->enter_loop()嵌套一个新的消息循环来阻塞当前事件的执行;然后在hide()函数里调用了qApp->exit_loop()来退出当前的消息循环并继续执行原事件。

到QT帮助里查了一下这两个函数,解释如下:
enter_loop():
这个函数被废弃了。它仍然被保留下来是为了使旧的代码能够继续工作。我们强烈建议不要在新写的代码里使用它。这个函数直接介入主消息循环里(递归地)。除非你真的知道你在做什么,否则不要调用它。建议使用:QApplication::eventLoop()->enterLoop()。
exit_loop():
同样被废弃了。建议使用:QApplication::eventLoop()->exitLoop()。
提醒:这两个操作都会进入主消息循环,慎用!

那就按照建议的做吧,反正效果都是一样的。修改原来的自定义窗口,在里面增加了两个函数,分别实现打开和关闭窗口,封装了eventLoop()的调用。代码如下:
#include <qapplication.h>
#include <qeventloop.h>

void MyWidget::doExec()
{
    this->show();
    in_loop = TRUE;
    //qApp->enter_loop();
    QApplication::eventLoop()->enterLoop();
}

void MyWidget::doClose()
{
    if ( in_loop ) {
in_loop = FALSE;
//qApp->exit_loop();
        QApplication::eventLoop()->exitLoop();
    }
    this->close();
}

D:\python313\python.exe C:\Users\86178\PycharmProjects\PythonProject\正统代码修炼\测试.py Traceback (most recent call last): File "C:\Users\86178\PycharmProjects\PythonProject\正统代码修炼\测试.py", line 262, in <module> w = WeatherWin() File "C:\Users\86178\PycharmProjects\PythonProject\正统代码修炼\测试.py", line 190, in __init__ self.status_label.setAlignment(Qt.AlignRight) ^^ NameError: name 'Qt' is not defined 进程已结束,退出代码为 1# desktop_weather_app.py import sys import csv from datetime import datetime import re import requests from bs4 import BeautifulSoup from PySide6.QtWidgets import ( QApplication, QWidget, QLabel, QLineEdit, QPushButton, QVBoxLayout, QHBoxLayout, QTableWidgetItem, QMessageBox, QTableWidget ) # ---------------- Spider ---------------- # 更新后的城市映射表,包含更多城市备用URL def chinese_to_cityinfo(name: str): name = name.strip() mapping = { # 格式:城市名: (拼音, 城市代码, 备用URL) "北京": ("beijing", "101010100", "bj"), "上海": ("shanghai", "101020100", "sh"), "广州": ("guangzhou", "101280101", "gz"), "深圳": ("shenzhen", "101280601", "sz"), "杭州": ("hangzhou", "101210101", "hz"), "南京": ("nanjing", "101190101", "nj"), "苏州": ("suzhou", "101190401", "su"), "武汉": ("wuhan", "101200101", "wh"), "成都": ("chengdu", "101270101", "cd"), "西安": ("xian", "101110101", "xa"), "重庆": ("chongqing", "101040100", "cq"), "天津": ("tianjin", "101030100", "tj"), "长沙": ("changsha", "101250101", "cs"), "合肥": ("hefei", "101220101", "hf"), "郑州": ("zhengzhou", "101180101", "zz"), "青岛": ("qingdao", "101120201", "qd"), "大连": ("dalian", "101070201", "dl"), "沈阳": ("shenyang", "101070101", "sy"), "厦门": ("xiamen", "101230201", "xm"), "福州": ("fuzhou", "101230101", "fz"), # 添加更多城市... } return mapping.get(name) def _first_text(soup: BeautifulSoup, selectors): """改进的选择器查找,支持正则匹配""" for sel in selectors: els = soup.select(sel) if isinstance(sel, str) else [soup.find(*sel)] for el in els: if el: txt = el.get_text(strip=True) if txt: return txt return None def _extract_temperature(text): """改进的温度提取,支持多种格式""" if not text: return None # 匹配数字℃/C符号 match = re.search(r'(-?\d+\.?\d*)\s*[℃°C]?', text) if match: return f"{match.group(1)}℃" return None def _extract_humidity(text): """改进的湿度提取""" if not text: return None # 匹配百分比数字 match = re.search(r'(\d+)\s*%?', text) if match: return f"{match.group(1)}%" return None def crawl_weather(city_cn: str): """ 改进的爬虫函数,支持多种页面结构备用URL 返回: dict 或 None """ info = chinese_to_cityinfo(city_cn) if not info: return None pinyin, code, short_name = info if len(info) == 3 else (*info, None) headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36", "Accept-Language": "zh-CN,zh;q=0.9", } # 准备要尝试的URL列表(按优先级排序) urls_to_try = [ f"https://www.weather.com.cn/weather1d/{code}.shtml", # PC端 f"https://e.weather.com.cn/d/top/{code}.shtml", # 备用PC端 f"https://m.weather.com.cn/mweather/{code}.shtml", # 移动端 f"https://m.weather.com.cn/mweather1d/{code}.shtml", # 移动端备用 ] if short_name: urls_to_try.extend([ f"https://{short_name}.weather.com.cn/weather1d/{code}.shtml", f"https://{short_name}.weather.com.cn/weather/{code}.shtml", ]) for url in urls_to_try: try: resp = requests.get(url, headers=headers, timeout=10) resp.raise_for_status() soup = BeautifulSoup(resp.text, "lxml") # 改进的选择器集合,覆盖更多可能的页面结构 temp_selectors = [ "span.tmp", "span.temperature", "span.tem", "i#current_temp", ".sk .tem span", ".tem.now .tem_num", ".weather .tem span", ".temp", ".temperature", ".now .temp", ".nowtemp", {"name": "div", "class": "tem"}, {"name": "div", "class": "temperature"} ] hum_selectors = [ "span.hum", "span.humidity", ".sk .sd", ".sk .hum", ".humidity", ".shidu", ".zs .sd", ".sd", {"name": "div", "class": "humidity"}, {"name": "div", "class": "shidu"} ] wind_selectors = [ "span.wind", "span.w", ".sk .wind", ".sk .win", ".wind", ".zs .win", ".zs .wind", ".win", {"name": "div", "class": "wind"}, {"name": "div", "class": "win"} ] temp_raw = _first_text(soup, temp_selectors) hum_raw = _first_text(soup, hum_selectors) wind_raw = _first_text(soup, wind_selectors) # 使用改进的数据提取函数 temp = _extract_temperature(temp_raw) humidity = _extract_humidity(hum_raw) # 风力处理 wind = None if wind_raw: wind = re.sub(r'[风力风向::]+', '', wind_raw).strip() if temp or humidity or wind: return { "city": city_cn, "time": datetime.now().strftime("%Y-%m-%d %H:%M"), "temp": temp or "—", "humidity": humidity or "—", "wind": wind or "—", } except Exception as e: print(f"尝试URL {url} 失败: {e}") continue return None # ---------------- GUI ---------------- class WeatherWin(QWidget): def __init__(self): super().__init__() self.setWindowTitle("桌面天气查询(增强版爬虫)") self.resize(800, 500) # 改进的UI布局 self.city = QLineEdit() self.city.setPlaceholderText("请输入中文城市名,例如:北京、上海、广州…") self.btn = QPushButton("查询") self.table = QTableWidget(0, 5) self.table.setHorizontalHeaderLabels(["城市", "时间", "温度", "湿度", "风力/风向"]) self.table.horizontalHeader().setStretchLastSection(True) self.table.setColumnWidth(0, 120) self.table.setColumnWidth(1, 150) self.table.setColumnWidth(2, 100) self.table.setColumnWidth(3, 100) # 添加状态标签 self.status_label = QLabel("就绪") self.status_label.setAlignment(Qt.AlignRight) # 布局 lay = QVBoxLayout(self) top = QHBoxLayout() top.addWidget(QLabel("城市:")) top.addWidget(self.city) top.addWidget(self.btn) lay.addLayout(top) lay.addWidget(self.table) lay.addWidget(self.status_label) # 连接信号 self.btn.clicked.connect(self.query) self.city.returnPressed.connect(self.query) # CSV文件 self.csv_file = "weather_history.csv" self._ensure_csv_header() def _ensure_csv_header(self): try: with open(self.csv_file, "r", encoding="utf-8") as f: has_header = f.readline() if has_header: return except FileNotFoundError: pass with open(self.csv_file, "w", encoding="utf-8", newline="") as f: writer = csv.writer(f) writer.writerow(["城市", "时间", "温度", "湿度", "风力/风向"]) def query(self): city = self.city.text().strip() if not city: QMessageBox.warning(self, "提示", "请输入城市名(中文)") return self.status_label.setText("正在查询中...") self.btn.setEnabled(False) QApplication.processEvents() # 更新UI data = crawl_weather(city) self.btn.setEnabled(True) if not data: self.status_label.setText("查询失败") QMessageBox.warning(self, "提示", "未能获取该城市天气数据,可能原因:\n" "1. 城市名称不在支持列表中\n" "2. 网站结构已更新\n" "3. 网络连接问题") return # 显示结果 self.status_label.setText("查询成功") row = self.table.rowCount() self.table.insertRow(row) for col, key in enumerate(["city", "time", "temp", "humidity", "wind"]): self.table.setItem(row, col, QTableWidgetItem(data[key])) # 写入CSV try: with open(self.csv_file, "a", encoding="utf-8", newline="") as f: writer = csv.writer(f) writer.writerow([data["city"], data["time"], data["temp"], data["humidity"], data["wind"]]) except Exception as e: QMessageBox.warning(self, "提示", f"写入CSV失败: {str(e)}") if __name__ == "__main__": app = QApplication(sys.argv) w = WeatherWin() w.show() sys.exit(app.exec())
最新发布
08-22
在使用 PyQt 或 PySide6 开发 GUI 应用程序时,开发者可能会遇到 `NameError: name 'Qt' is not defined` 的错误提示。这通常是因为没有正确导入 Qt 类或者模块导致的。 在 PySide6 中,`Qt` 类通常是从 `PySide6.QtCore` 或其他相关模块中导入的。如果未正确导入,Python 解释器将无法识别该名称,从而引发 `NameError`。解决这个问题的关键在于确保所有使用的 Qt模块都已正确导入。 以下是几种常见的解决方案: ### 显式导入 Qt 类 ```python from PySide6.QtCore import Qt ``` 通过显式地从 `PySide6.QtCore` 导入 `Qt` 类,可以确保在代码中使用 `Qt` 时不会出现未定义的错误[^1]。 ### 检查导入语句是否正确 确认导入语句是否正确无误,特别是在从多个模块导入时。例如: ```python from PySide6.QtWidgets import QApplication, QLabel from PySide6.QtCore import Qt ``` ### 使用通配符导入(不推荐) 虽然不推荐使用通配符导入,因为它可能导致命名空间污染,但在某些情况下,可以使用以下方式导入所有内容: ```python from PySide6.QtCore import * ``` 这种方式会导入 `PySide6.QtCore` 模块中的所有类函数,包括 `Qt` 类。 ### 示例代码 下面是一个简单的示例,展示了如何正确导入 `Qt` 类并使用它来设置窗口标志: ```python import sys from PySide6.QtWidgets import QApplication, QWidget, QLabel from PySide6.QtCore import Qt app = QApplication(sys.argv) window = QWidget() window.setWindowTitle("Qt Import Example") window.resize(300, 200) label = QLabel("Hello, Qt!", window) label.move(100, 80) label.setAlignment(Qt.AlignCenter) # 使用 Qt 类设置文本对齐方式 window.show() sys.exit(app.exec()) ``` 在这个例子中,`Qt` 类被用来设置标签的对齐方式,通过 `label.setAlignment(Qt.AlignCenter)` 实现[^1]。 ### 总结 解决 `NameError: name 'Qt' is not defined` 的关键是确保 `Qt` 类已经被正确导入。通过显式导入所需的类或模块,可以避免此类错误的发生。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值