如何将QPixmap保存为具有C ++和Qt的文件夹中的图片?

时间:2016-07-29 07:35:01

标签: c++ qt

我正在尝试编写一个C ++函数,使用Qt GUI将选定的图片保存到我的程序目录中。

到目前为止,我在QPixmap对象上使用的“保存”功能不会保存任何内容,我无法弄清楚原因。

以下是代码:

def get_width(b):
    if b <= 0x7f:
        return 1
    elif 0x80 <= b <= 0xbf:
        #Continuation byte
        raise ValueError('Bad alignment: %r is a continuation byte' % b)
    elif 0xc0 <= b <= 0xdf:
        return 2
    elif 0xe0 <= b <= 0xef:
        return 3
    elif 0xf0 <= b <= 0xf7:
        return 4
    else:
        raise ValueError('%r is not a single byte' % b)

def decode_utf8(utfbytes):
    start = 0
    uni = []
    while start < len(utfbytes):
        b = utfbytes[start]
        w = get_width(b)
        if w == 1:
            n = b
        else:
            n = b & (0x7f >> w)
            for b in utfbytes[start+1:start+w]:
                if not 0x80 <= b <= 0xbf:
                    raise ValueError('Not a continuation byte: %r' % b)
                n <<= 6
                n |= b & 0x3f
        uni.append(chr(n))
        start += w
    return ''.join(uni)


utfbytes = b'\xc2\xa9 \xc2\xae \xe2\x84\xa2'
print(utfbytes.decode('utf8'))
print(decode_utf8(utfbytes))

任何人都可以帮助我吗? :)

1 个答案:

答案 0 :(得分:1)

  • QDir::currentPath()返回当前正在运行的目录。显然,未指定文件名本身。只需附加所需的文件名,例如:QDir::currentPath() + "/123.png"

  • QPixmap::toImage()是一个const方法,返回从QImage转换的QPixmap。它确实对您的代码没有任何用处,删除它或使用QImage代替。

  • QDir::currentPath()返回当前工作目录,必然是应用程序可执行目录。如果您需要可执行目录,请使用QCoreApplication::applicationDirPath()
  • 另外,正如 Violet Giraffe 所指出的那样,可能存在写入权限问题。