首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >qt pixmap.save不工作

qt pixmap.save不工作
EN

Stack Overflow用户
提问于 2014-09-07 09:10:25
回答 3查看 1.3K关注 0票数 1

在我的截图拍摄项目中,QPixmap.save()函数每次都返回假意义失败。但是,当我从Qt页面http://qt-project.org/doc/qt-5/qtwidgets-desktop-screenshot-example.html复制示例项目时,它可以工作。因此,它排除了Windows 7文件权限的问题。

所以我想知道为什么失败了?

Wdget.h文件:

代码语言:javascript
复制
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

代码语言:javascript
复制
#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);
}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-09-07 10:22:49

代码语言:javascript
复制
QDateTime local( QDateTime::currentDateTime() ) 

可能包含Windows不允许的符号。(符号很少)。所以你救不了它。

解决方案:首先,尝试从文件名中删除dateTime并查看它是否有效。如果要使用dateTime,请尝试在没有禁止符号的情况下格式化它

例如,Windows中禁止的符号:

代码语言:javascript
复制
< (less than)
> (greater than)
: (colon)
" (double quote)
/ (forward slash)
\ (backslash)
| (vertical bar or pipe)
? (question mark)
* (asterisk)

QDateTime总是返回包含冒号的字符串,但是它是被禁止的,您不能使用它,您应该替换它。

票数 2
EN

Stack Overflow用户

发布于 2014-09-07 10:40:05

它只在linux下工作。除了@切尔诺贝利的答案外,AFAIK QPixmap::save不会自动添加后缀,因此您需要更改

代码语言:javascript
复制
QString fileName = initialPath + date;

代码语言:javascript
复制
QString fileName = initialPath + date.replace(":", "-") + ".png";

( .replace(":", "-")部分用于转义禁止的":“文件名中的符号)

票数 2
EN

Stack Overflow用户

发布于 2014-09-07 10:40:19

当您构建文件名及其路径时,如下所示:

代码语言:javascript
复制
QString fileName = initialPath + date;

您将有一条类似于

C:/User/YourName/Pictures

为你的第一条路。

而您的约会将以

2014年9月7日11:35:46

所以在你的连接过程中,你最终会得到这样的结果

C:/User/YourName/PicturesSun 9月7日11:35:46 2014

如果你仔细观察,这里有很多问题:

  1. 您在初始路径的末尾缺少一个"/“
  2. 日期字符串包含Windows不允许文件名的字符
  3. 一旦保存,文件将失去其扩展名,这将需要添加到您的文件名字符串的末尾。

所需的修正:

您需要将日期格式更改为Windows可以接受的格式,方法是

QString date =local.toString(“windows的有效QDateTime格式”) QString fileName = initialPath +“/+日期+”。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25708673

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档