我有一些Traceback (most recent call last): File "C:\Python\lib\site-packages\requests\packages\urllib3\connection.py", line 135, in _new_conn
(self.host, self.port), self.timeout, **extra_kw) File "C:\Python\lib\site-packages\requests\packages\urllib3\util\connection.py", line 66, in create_connection
for res in socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM): File "C:\Python\lib\socket.py", line 732, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags): socket.gaierror: [Errno 11001] getaddrinfo failed
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File "C:\Python\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 559, in urlopen
body=body, headers=headers) File "C:\Python\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 345, in _make_request
self._validate_conn(conn) File "C:\Python\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 782, in _validate_conn
conn.connect() File "C:\Python\lib\site-packages\requests\packages\urllib3\connection.py", line 215, in connect
conn = self._new_conn() File "C:\Python\lib\site-packages\requests\packages\urllib3\connection.py", line 144, in _new_conn
self, "Failed to establish a new connection: %s" % e) requests.packages.urllib3.exceptions.NewConnectionError: <requests.packages.urllib3.connection.VerifiedHTTPSConnection object at 0x00000132FB57A438>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File "C:\Python\lib\site-packages\requests\adapters.py", line 370, in send
timeout=timeout File "C:\Python\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 609, in urlopen
_stacktrace=sys.exc_info()[2]) File "C:\Python\lib\site-packages\requests\packages\urllib3\util\retry.py", line 271, in increment
raise MaxRetryError(_pool, url, error or ResponseError(cause)) requests.packages.urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='translate.google.com', port=443): Max retries exceeded with url: / (Caused by NewConnectionError('<requests.packages.urllib3.connection.VerifiedHTTPSConnection object at 0x00000132FB57A438>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed',))
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File "<pyshell#3>", line 1, in <module>
tts.save('Hello.mp3') File "C:\Python\lib\site-packages\gtts\tts.py", line 94, in save
self.write_to_fp(f) File "C:\Python\lib\site-packages\gtts\tts.py", line 107, in write_to_fp
'tk' : self.token.calculate_token(part)} File "C:\Python\lib\site-packages\gtts_token\gtts_token.py", line 28, in calculate_token
seed = self._get_token_key() File "C:\Python\lib\site-packages\gtts_token\gtts_token.py", line 58, in
_get_token_key
response = requests.get("https://translate.google.com/") File "C:\Python\lib\site-packages\requests\api.py", line 69, in get
return request('get', url, params=params, **kwargs) File "C:\Python\lib\site-packages\requests\api.py", line 50, in request
response = session.request(method=method, url=url, **kwargs) File "C:\Python\lib\site-packages\requests\sessions.py", line 468, in request
resp = self.send(prep, **send_kwargs) File "C:\Python\lib\site-packages\requests\sessions.py", line 576, in send
r = adapter.send(request, **kwargs) File "C:\Python\lib\site-packages\requests\adapters.py", line 423, in send
raise ConnectionError(e, request=request) requests.exceptions.ConnectionError: HTTPSConnectionPool(host='translate.google.com', port=443): Max retries exceeded with url: / (Caused by NewConnectionError('<requests.packages.urllib3.connection.VerifiedHTTPSConnection object at 0x00000132FB57A438>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed',))
个对象在它们和工具栏之间共享一些QMenu
。
这里的细节是我想在其他菜单上重现一些菜单部分。例如,在我的QAction
对象的MenuBar中,我有类似
QMainWindow
然后,在某些上下文菜单中,我想使用相同的操作对象,仅使用标有File
(x) New
(x) Open
(x) Save
(x) ------
(x) Add Sketch...
(-) Export...
(-) Import...
的操作重现相同的确切结构。我想在几个菜单上做到这一点。
天真的方法是在构建(x)
菜单和重复结构的上下文菜单时重复相同的代码。但这意味着重复代码,从而使维护工作量增加一倍(重复菜单操作的次数)。
有没有直接的方法在Qt上解决这个问题?
答案 0 :(得分:1)
一个选项可以是将操作存储在字典中,然后为每个子菜单保存不同的键列表。您以后所要做的就是使用简单的for来构建菜单。草案代码可能如下所示:
QMap<QString, QAction*> actions;
actions["new"] = new QAction(...); // connect, etc.
actions["-"] = new QAction(...); // for separator
QMap<QString, QStringList> menus;
menus["file"] << "new" << "save" << ... << "import" << ...;
menus["context1"] << "new" << "save" << ...;
QMenu* buildMenu(const QString& name) {
if (!menus.contains(name)) return nullptr;
auto menu = new QMenu();
Q_FOREACH (const auto& entry, menus[name]) {
menu->addAction(actions[entry]);
}
return menu;
}
最后,将actions
和menus
提供给buildMenu
(一个类,匿名命名空间一起初始化......)。还记得在使用后删除返回的菜单。