在我的截图拍摄项目中,QPixmap.save()函数每次都返回假意义失败。但是,当我从Qt页面http://qt-project.org/doc/qt-5/qtwidgets-desktop-screenshot-example.html复制示例项目时,它可以工作。因此,它排除了Windows 7文件权限的问题。
所以我想知道为什么失败了?
Wdget.h文件:
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
private slots:
void updateTimer();
void startScreenshots();
void stopScreenshots();
void takeScreenshot();
private:
Ui::Widget *ui;
QString initialPath;
QPixmap currentScreenshot;
QSpinBox * delaySpinBox;
QPushButton * startButton;
QPushButton * stopButton;
QHBoxLayout * hboxLayout;
QGroupBox * groupBox;
QTimer * timer;
void setInitialPath();
void addStuff();
};widget.cpp
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
setInitialPath();
addStuff();
}
Widget::~Widget()
{
delete ui;
}
void Widget::updateTimer()
{
timer->stop();
int milisecs = delaySpinBox->value() *1000;
timer->start( milisecs );
}
void Widget::startScreenshots()
{
timer->start( delaySpinBox->value() * 1000 );
}
void Widget::stopScreenshots()
{
timer->stop();
}
void Widget::takeScreenshot()
{
//take screenshot
currentScreenshot = QPixmap::grabWindow(QApplication::desktop()->winId());
//save screenshot
QString format = "png";
QDateTime local( QDateTime::currentDateTime() );
QString date = local.toString();
QString fileName = initialPath + date;
if(!currentScreenshot.save(fileName, format.toLatin1().constData()) )
{
qDebug() << "didnt save\n";
QMessageBox::information(this,"failed to save","failed to save");
}
}
void Widget::setInitialPath()
{
initialPath = QFileDialog::getExistingDirectory(this, tr("Open Directory"),
"/home",
QFileDialog::ShowDirsOnly
| QFileDialog::DontResolveSymlinks);
}
void Widget::addStuff()
{
timer = new QTimer(this);
connect(timer,SIGNAL(timeout()),this,SLOT(takeScreenshot()) );
delaySpinBox = new QSpinBox(this);
delaySpinBox->setValue(60);
delaySpinBox->setSuffix(tr(" s"));
connect( delaySpinBox,SIGNAL(valueChanged(int)),this,SLOT(updateTimer()) );
startButton = new QPushButton(this);
startButton->setText("start");
connect(startButton,SIGNAL(clicked()),this,SLOT(startScreenshots()) );
stopButton = new QPushButton(this);
stopButton->setText("stop");
connect(stopButton,SIGNAL(clicked()),this,SLOT(stopScreenshots()) );
hboxLayout = new QHBoxLayout(this);
hboxLayout->addWidget(startButton);
hboxLayout->addWidget(stopButton);
hboxLayout->addWidget(delaySpinBox);
groupBox = new QGroupBox(tr("Options"));
groupBox->setLayout(hboxLayout);
setLayout(hboxLayout);
}发布于 2014-09-07 10:22:49
QDateTime local( QDateTime::currentDateTime() ) 可能包含Windows不允许的符号。(符号很少)。所以你救不了它。
解决方案:首先,尝试从文件名中删除dateTime并查看它是否有效。如果要使用dateTime,请尝试在没有禁止符号的情况下格式化它
例如,Windows中禁止的符号:
< (less than)
> (greater than)
: (colon)
" (double quote)
/ (forward slash)
\ (backslash)
| (vertical bar or pipe)
? (question mark)
* (asterisk)QDateTime总是返回包含冒号的字符串,但是它是被禁止的,您不能使用它,您应该替换它。
发布于 2014-09-07 10:40:05
它只在linux下工作。除了@切尔诺贝利的答案外,AFAIK QPixmap::save不会自动添加后缀,因此您需要更改
QString fileName = initialPath + date;至
QString fileName = initialPath + date.replace(":", "-") + ".png";( .replace(":", "-")部分用于转义禁止的":“文件名中的符号)
发布于 2014-09-07 10:40:19
当您构建文件名及其路径时,如下所示:
QString fileName = initialPath + date;您将有一条类似于
C:/User/YourName/Pictures
为你的第一条路。
而您的约会将以
2014年9月7日11:35:46
所以在你的连接过程中,你最终会得到这样的结果
C:/User/YourName/PicturesSun 9月7日11:35:46 2014
如果你仔细观察,这里有很多问题:
所需的修正:
您需要将日期格式更改为Windows可以接受的格式,方法是
QString date =local.toString(“windows的有效QDateTime格式”) QString fileName = initialPath +“/+日期+”。
https://stackoverflow.com/questions/25708673
复制相似问题