小编han*_*dle的帖子

QDateTime::fromstring( __DATE__, "MMM d yyyy") 返回无效

使用 QDateTime::fromstring()解析 MSVC++ 预定义__DATE__(可能与__TIME__)宏不会返回任何内容(= 无效的 QDateTime 对象)。为什么?

c++ qt visual-c++

5
推荐指数
1
解决办法
3120
查看次数

C++ Qt QShortcut with numpad key

QShortcut可以轻松地将QShortcutEvent(按键,组合或序列)连接到插槽方法,例如:

QShortcut *shortcut = new QShortcut( QKeySequence(Qt::Key_7), this, 0, 0, Qt::ApplicationShortcut);
Run Code Online (Sandbox Code Playgroud)

(提示:对于数字键,可以使用QSignalMapper将QShortcut的activated()信号映射到带int参数的Slot ).

但是,在此示例中,使用NumLock(启用了numpad),两个"7"键都将触发Shortcut的activated()信号.

有没有一种方法来检测比过滤或重新实现Widget的其他不同的键keyPressEvent和检查QKeyEvent ::修饰符()Qt的:: KeypadModifier

我发现,进一步挖掘

QTBUG-20191的Qt :: KeypadModifier不setShortcut工作链接到已在2012年九月被合并到4.8补丁,并且配备了一个测试用例使用

button2->setShortcut(Qt::Key_5 + Qt::KeypadModifier);
Run Code Online (Sandbox Code Playgroud)

这对Qt 4.8.1上的QShortcut不起作用,即使用(添加)修饰符标志都不能识别'7'键.

所以我想最快的方法是安装一个过滤器来检测修饰符,并让默认实现处理所有其他的keyEvent,以便与QShortcut一起使用?

c++ qt keyboard-shortcuts

5
推荐指数
1
解决办法
4454
查看次数

如何在其构造函数(Qt GUI)之后运行类方法?

在程序的 main() 函数中构造并显示 QMainwindow 对象。该对象的构造器用于创建所有 GUI 小部件。它包含当前在 QMainWindow 小部件可见之前运行的附加代码(或方法调用)。

此代码/方法应在 QMainWindow 构造函数之后运行一次,即当应用程序窗口可见时。

根据showEvent的文档,它可能会运行多次。

我是否需要在此事件中使用某种切换标志,或者是否有“更好”的解决方案(我以为我读到 QTimer 可用于将方法排队到事件循环中)?

c++ user-interface qt

5
推荐指数
1
解决办法
1733
查看次数

如何在 Windows 命令行上通过 html-minifier 将选项传递给 UglifyJS?

用于 Node.js (v8.11.1) 的HTMLMinifier ( html-minifier ) (3.5.14),随 一起安装,可以通过命令行 ( Windows CMDnpm install html-minifier -g )运行,例如生成使用信息(摘录):html-minifier --help

  Usage: html-minifier [options] [files...]

  Options:

    -V, --version                        output the version number
Run Code Online (Sandbox Code Playgroud)

...

    --minify-js [value]                  Minify Javascript in script elements and on* attributes (uses uglify-js)
Run Code Online (Sandbox Code Playgroud)

...

    -c --config-file <file>              Use config file
    --input-dir <dir>                    Specify an input directory
    --output-dir <dir>                   Specify an output directory
    --file-ext <text>                    Specify an extension to be read, ex: html
    -h, --help                           output usage information
Run Code Online (Sandbox Code Playgroud)

该选项--minify-js [value] …

json cmd escaping minify node.js

5
推荐指数
1
解决办法
2154
查看次数

如何切换到Windows上的其他应用程序(使用C ++,Qt)?

我想让我的GUI用户(GI用户?)能够直接切换到已知的友好应用程序,例如通过键盘快捷键。理想情况下,我的应用程序将要求OS / Windows按名称或主窗口标题字符串“ XYZ”显示应用程序。

手动操作路径为ALT + TAB,以打开Windows Task Switcher,然后定位并导航到所需的应用程序图标,最终将其带入活动程序窗口的前台。或者,通过任务栏导航。

AutoHotkey有一个功能WinActivate,可以实现我想要的功能。

c++ windows qt

4
推荐指数
1
解决办法
4328
查看次数

Python线程self._stop()'Event'对象不可调用

/sf/answers/22786991/尝试"可停止"的线程,如下所示:

import sys
import threading
import time
import logging

class StoppableThread(threading.Thread):
    """Thread class with a stop() method. The thread itself has to check
    regularly for the stopped() condition."""

    def __init__(self):
        print( "base init", file=sys.stderr )
        super(StoppableThread, self).__init__()
        self._stop = threading.Event()

    def stop(self):
        print( "base stop()", file=sys.stderr )
        self._stop.set()

    def stopped(self):
        return self._stop.is_set()


class datalogger(StoppableThread):
    """
    """

    import time

    def __init__(self, outfile):
      """
      """
      StoppableThread.__init__(self)
      self.outfile = outfile
      print( "thread init", file=sys.stderr )

    def run(self):
      """
      """
      print( "thread …
Run Code Online (Sandbox Code Playgroud)

python multithreading python-3.4

4
推荐指数
1
解决办法
6630
查看次数

正则表达式只捕获重复组的最后一次出现

我试图从这样的字符串中使用Python 正则表达式捕获多个 "<attribute> = <value>" 对:

  some(code) ' <tag attrib1="some_value" attrib2="value2"                   en=""/>
Run Code Online (Sandbox Code Playgroud)

正则表达式'\s*<tag(?:\s*(\w+)\s*=\"(.*?)\")*旨在多次匹配这些对,即返回类似

"attrib1", "some_value", "attrib2", "value2", "en", ""
Run Code Online (Sandbox Code Playgroud)

但它只捕获最后一次出现:

>>> import re
>>> re.search("'\s*<tag(?:\s*(\w+)\s*=\"(.*?)\")*", '  some(code) \' <tag attrib1="some_value" attrib2="value2"                   en=""/>').groups()
('en', '')
Run Code Online (Sandbox Code Playgroud)

专注于 <attrib>="<value>" 作品:

>>> re.findall("(?:\s*(\w+)\s*=\"(.*?)\")", '  some(code) \' <tag attrib1="some_value" attrib2="value2"                   en=""/>')
[('attrib1', 'some_value'), ('attrib2', 'value2'), ('en', '')]
Run Code Online (Sandbox Code Playgroud)

所以一个实用的解决方案可能是"<tag" in string在运行这个正则表达式之前进行测试,但是..

为什么原始正则表达式只捕获最后一次出现的情况以及需要更改哪些内容才能使其按预期工作?

python regex

4
推荐指数
1
解决办法
1393
查看次数

使用字符串元组更新 Python 字典以设置(键,值)失败

dict.update([other])

使用其他的键/值对更新字典,覆盖现有的键。返回

update() 接受另一个字典对象或键/值对的可迭代对象(作为元组或长度为 2 的其他可迭代对象)。如果指定了关键字参数,则字典将使用这些键/值对进行更新:d.update(red=1, blue=2)。

>>> {}.update( ("key", "value") )
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: dictionary update sequence element #0 has length 3; 2 is required
Run Code Online (Sandbox Code Playgroud)

那么为什么 Python 显然会尝试使用元组的第一个字符串呢?

python grammar dictionary iterable

3
推荐指数
1
解决办法
2919
查看次数

从 email.message 中的字符串设置电子邮件内容?

我想使用 Python 发送电子邮件。

有 sendmail (通过 python 的 sendmail 发送邮件),还有https://docs.python.org/3/library/smtplib.html它建议基于https://docs.python.org/3/library/email.message.html构建消息,并有一些示例https://docs.python.org/3/library/email.examples.html #email-examples从文件中读取消息内容:

# Open the plain text file whose name is in textfile for reading.
with open(textfile) as fp:
    # Create a text/plain message
    msg = EmailMessage()
    msg.set_content(fp.read())
Run Code Online (Sandbox Code Playgroud)

我试过了

msg.set_content(b"test message sent locally")
Run Code Online (Sandbox Code Playgroud)

但这会导致TypeError: set_bytes_content() missing 2 required positional arguments: 'maintype' and 'subtype'. 似乎https://docs.python.org/3/library/email.message.html#email.message.EmailMessage.set_content需要上下文管理器?

如何使用字符串来构造消息正文?

python email

3
推荐指数
1
解决办法
5699
查看次数

Qt GUI用户与QThread对象内的QMessageBox交互

我正在使用QThread MyObject->moveToThread(myThread);来进行需要一段时间的通信功能.一些信号和插槽保持GUI发布有关进度.

但是,在需要用户交互的线程通信期间可能会出现某些情况 - 因为无法在线程内创建QMessageBox,我想发出一个信号,允许我暂停线程并显示对话框.但首先,似乎没有办法暂停一个线程,其次,这种尝试可能会失败,因为它需要一种方法在恢复时将参数传递回线程.

一个不同的方法可能是事先将所有参数传递给线程,但这可能不是一个选项.

这通常是怎么做的?

编辑

感谢#1的评论,并希望得到我的希望,但请详细说明如何从线程中的对象创建例如对话框以及如何暂停它.

使用Qt 4.8.1和MSVC++ 2010的以下示例代码导致:

MyClass::MyClass created 
MainWindow::MainWindow thread started 
MyClass::start run 
ASSERT failure in QWidget: "Widgets must be created in the GUI thread.", file kernel\qwidget.cpp, line 1299
Run Code Online (Sandbox Code Playgroud)

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H
Run Code Online (Sandbox Code Playgroud)

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include "myclass.h"
#include <QThread>
#include …
Run Code Online (Sandbox Code Playgroud)

user-interface qt multithreading

2
推荐指数
1
解决办法
2842
查看次数

像素坐标与绘图坐标

在下面的代码片段中,传递 x 和 y 值会将点放在 (y,x) 坐标中,而绘图是在 (x,y) 中完成的。设置绘图缓冲区以便在同一坐标系中放置像素和绘图的正确方法是什么?

from PIL import Image, ImageDraw

def visual_test(x, y):
    grid = np.zeros((100, 100, 3), dtype=np.uint8)
    grid[:] = [0, 0, 0]
    grid[x, y] = [255, 0, 0]
    img = Image.fromarray(grid, 'RGB')
    draw = ImageDraw.Draw(img)
    draw.line((x, y, x, y-5), fill=(255,255,255), width=1)
    img.show()
Run Code Online (Sandbox Code Playgroud)

python numpy python-imaging-library pillow

2
推荐指数
1
解决办法
7394
查看次数

Qt QGraphicsScene 不适合滚动条的 InView()

QGraphicsView::fitInView()似乎忽略了滚动条的存在,滚动条显然是重叠的。(它还使用硬编码的 2 像素边距。)

有一个相关的错误报告 ( https://bugreports.qt-project.org/browse/QTBUG-1047 ) 指出调用 fitInView() 两次可以解决问题。

就我而言,它没有。仅手动触发它两次适合滚动条。这不起作用:

void myGraphicsView::mousePressEvent(QMouseEvent *event) {
  if( event->button() == Qt::LeftButton ) {
    QGraphicsItem* clicked = scene()->itemAt( mapToScene( event->pos() ) );
    qDebug() << clicked << clicked->boundingRect();
    fitInView( clicked, Qt::KeepAspectRatio);
    fitInView( clicked, Qt::KeepAspectRatio); // doesn't work for me
    QGraphicsView::mousePressEvent(event);
    return;
  }
}
Run Code Online (Sandbox Code Playgroud)

还有其他解决方法吗?

Qt 4.8.1 与 MSVC2010

c++ qt scrollbar qgraphicsview

1
推荐指数
1
解决办法
765
查看次数

迭代QVariant是QList <int>?

我正在使用QObject的动态属性来存储要在可以访问所述属性的Slot中使用的信息.发件人是一个QState:myQState->setProperty("key", QList<int>(0, 1, 2));

我想将存储的QVariant转换回QList,以便可以迭代.以下代码不起作用(错误C2440:QVariant无法使用{[T = int])转换为QList:

QVariant vprop = obj->property("key");
QList<int> list = vprop; //   < - - - - - - - - ERROR
foreach (int e, list )
{
    qDebug() << __FUNCTION__ << "" << e;
}
Run Code Online (Sandbox Code Playgroud)

这段代码有效.要设置为属性的对象:

QVariantList list;
list.append(0);
list.append(1);
list.append(2);
Run Code Online (Sandbox Code Playgroud)

并在插槽中

QObject *obj = this->sender();
foreach( QByteArray dpn, obj->dynamicPropertyNames() )
{
    qDebug() << __FUNCTION__ << "" << dpn;
}
QVariant vprop = obj->property("key");
qDebug() << __FUNCTION__ << "" << vprop;
QVariantList …
Run Code Online (Sandbox Code Playgroud)

c++ qt casting qvariant qlist

0
推荐指数
1
解决办法
3186
查看次数