2017-10-04 235 views
-1

给错误screenshot.save('screenshot.png', 'PNG');QT5 - 无法从 '诠释' 转换参数2“为const char *

..\screen\main.cpp(10): error C2015: too many characters in constant 
..\screen\main.cpp(10): error C2664: 'bool QPixmap::save(QIODevice *,const char *,int) const': cannot convert argument 2 from 'int' to 'const char *' 

代码:

#include "mainwindow.h" 
#include <QApplication> 
#include <QScreen> 
#include <QPixmap> 

int main(int argc, char *argv[]) { 
    QApplication a(argc, argv); 
    QScreen *screen = a.primaryScreen(); 
    QPixmap screenshot = screen->grabWindow(0); 
    screenshot.save('screenshot.png', 'PNG'); 

    MainWindow w; 
    w.show(); 
    return a.exec(); 
} 
+3

你必须使用'“'的字符串,而不是''' 。 – nwp

+3

C++不是Python,不要假设,[学习它](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – Drop

+0

[这解释](http: //en.cppreference.com/w/cpp/language/character_literal)int是从哪里来的。 – nwp

回答

3

变化

screenshot.save('screenshot.png', 'PNG'); 

screenshot.save("screenshot.png", "PNG"); 

您可以在单个字符中使用撇号,如'a'。 如果函数需要的const char *参数类型,你必须使用双引号(“),即使你写有单个字符,如‘一’

‘一’ - 类型为const char的

‘一’ - 类型为const char *相关

如果你写里面撇多个字符,编译器无法将其转换为char,将转换为int。

+1

更确切地说'a'是char,'screenshot.png'不能表示为char,因此它被表示为int。 – deW1

+0

我已经更新了我的答案 –

相关问题