c++ - qt pixmap.save 不工作

标签 c++ qt qpixmap

在我的截屏项目中,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/

相关文章:

c++ - Cuda 基本程序(将值写入矩阵和 std :cout does not work) ; Main function does not start

c++ - 在 Raspbian lite 上运行 Qt Quick (EGLFS)?

c++ - 减小 QPixmap 的文件大小

qt - 放大缩小的 QPixmap : can't restore original size

使用宏的任意成员变量的c++类生成

c++ - 通过重载 < 对自定义对象的 vector 进行排序

c++ - 将Leap Motion Controller 与QT创建器一起使用

qt - 如何绘制带有圆角的QPixmap?

c++ - 在 C++ 中是否有类似的方法从 stdin 读取整数对到 vector<pair<int,int>>

c++ - QVariant 包装指针在 QML 中变为 null