在我的截屏项目中,QPixmap.save() 函数每次都返回 false 意味着失败。然而,当我从 Qt 页面复制示例项目时 http://qt-project.org/doc/qt-5/qtwidgets-desktop-screenshot-example.html , 有用。因此它排除了 Windows 7 的文件权限问题。
所以我想知道为什么它失败了?
widget.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();
};
小部件.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);
}
最佳答案
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总是返回包含冒号的字符串,但它是被禁止的,你不能使用它,你应该替换它。
关于c++ - qt pixmap.save 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25708673/