PDA

View Full Version : [Qt5] Set Default Suffix



Salads
14th February 2013, 04:12
I made a small program that saves a file, but people who use Ubuntu say that they can't properly save the file without manually putting a file extension in the save file name. I tried using the setDefaultSuffix function though I don't see how it would fit with the getSaveFileName function that I'm using.

Here's my code for the save button:


void MainWindow::on_save_clicked()
{
if (pngData.isEmpty())
{
QPixmap noSaveData(":/graphic/noSaveData.png");
ui->preview->setStyleSheet("background: transparent ;");
ui->preview->setPixmap(noSaveData);
return;
}
QString filename = QFileDialog::getSaveFileName(this, ("Save Skin"), "C:/Users/", ("PNG Image (*.png)"));
QFile file(filename);
file.open(QIODevice::WriteOnly);
file.write(pngData);
file.close();
}

alrawab
14th February 2013, 04:36
QFileDialog::getSaveFileName(0,"Save Skin",QDir::homePath(),"PNG Image (*.png)");
there is no C:/Users in Linux ...read The Current Directory and Other Special Paths
QDir

Salads
14th February 2013, 04:48
Thanks for the tip, the save function now goes to the directory I want, however I get weird error output since I added it in:
8717


QFSFileEngine::open: No file name specified
Cannot create accessible interface for object: MainWindow(0x33fbd4, name = "MainWindow")
Cannot create accessible interface for object: QPushButton(0x8f4880, name = "save")
Cannot create accessible interface for object: MainWindow(0x33fbd4, name = "MainWindow")
Cannot create accessible interface for object: MainWindow(0x33fbd4, name = "MainWindow")
Cannot create accessible interface for object: QPushButton(0x8f4880, name = "save")
Cannot create accessible interface for object: QPushButton(0xa77478, name = "dLoad")
Cannot create accessible interface for object: QLineEdit(0xa774d8, name = "lineEdit")
Cannot create accessible interface for object: QPushButton(0x8f4880, name = "save")
Cannot create accessible interface for object: QLabel(0x8f4900, name = "preview")
Cannot create accessible interface for object: QPushButton(0x8f4960, name = "clear")
Cannot create accessible interface for object: QWidget(0xa77418, name = "centralWidget")
Cannot create accessible interface for object: MainWindow(0x33fbd4, name = "MainWindow")
Cannot create accessible interface for object: MainWindow(0x33fbd4, name = "MainWindow")


Everything seems to be working regardless, besides the missing extensions that Linux users have.

EDIT: The errors don't seem related to QDir, as I changed it back just to check and had the same result.

alrawab
14th February 2013, 05:18
its works fine if we supposed pngData is correct data type


#include <QApplication>
#include <QFileDialog>

int main(int argc, char **argv)
{
QApplication app(argc,argv);
QString filename =QFileDialog::getSaveFileName(0,"Save Skin",QDir::homePath(),"PNG Image (*.png)");
QFile file(filename);
file.open(QIODevice::WriteOnly);
file.write("Foo");//change Foo to your data

file.close();
return 0;
}

QPixmap::save

Salads
14th February 2013, 07:07
Added after 4 minutes:

I think I want to keep it as a QByteArray because I use the .clear() functions for a clear button in the downloader. (It's a small minecraft skin downloader)
Would the QFileDialog::setDefaultSuffix work here? How would I implement it?

Here's my full code:

header:


#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QNetworkReply>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
QByteArray pngData;
QNetworkAccessManager *manager;


private slots:
void on_dLoad_clicked();

void replyFinished(QNetworkReply *reply);

void on_save_clicked();

void on_clear_clicked();

void on_lineEdit_returnPressed();

private:
Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H


mainwindow.cpp


#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QIcon>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QDebug>
#include <QFileDialog>
#include <QFile>
#include <QDir>

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
setWindowTitle("Minecraft Skin Downloader v0.7"); //Version
QIcon icon(":/graphic/ico.png");
setWindowIcon(icon);
setFixedSize(400, 200);
setStyleSheet("QMainWindow { background-image: url( :/graphic/bg.png); } ");
setWindowFlags(windowFlags() ^ Qt::WindowMaximizeButtonHint);
ui->lineEdit->setToolTip("Username (Case Sensitive)");

manager = new QNetworkAccessManager(this);
connect(manager, SIGNAL(finished(QNetworkReply *)), this, SLOT(replyFinished(QNetworkReply *)));
}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::on_dLoad_clicked()
{
//s3.amazonaws.com/MinecraftSkins/NICKNAME.png
QString prefix = "http://s3.amazonaws.com/MinecraftSkins/";
QString usrl = prefix += ui->lineEdit->text() += ".png";

QUrl url(usrl);
QNetworkRequest request(url);

manager->get(request);
}

void MainWindow::replyFinished(QNetworkReply *reply)
{
if(reply->error() != QNetworkReply::NoError)
{
qDebug() <<"Error in" << reply->url() << ":" << reply->errorString();
QPixmap noUsr(":/graphic/voidUser.png");
ui->preview->setStyleSheet("background: transparent ;");
ui->preview->setPixmap(noUsr);
pngData.clear();
return;
}

pngData = reply->readAll();
QPixmap srcPic;
srcPic.loadFromData(pngData);
QPixmap scaled = srcPic.scaled(128, 64, Qt::KeepAspectRatio, Qt::FastTransformation);
ui->preview->setStyleSheet("QLabel { background-color: white ;}");
ui->preview->setPixmap(scaled);

}

void MainWindow::on_save_clicked()
{
if (pngData.isEmpty())
{
QPixmap noSaveData(":/graphic/noSaveData.png");
ui->preview->setStyleSheet("background: transparent ;");
ui->preview->setPixmap(noSaveData);
return;
}
QString filename = QFileDialog::getSaveFileName(this, tr("Save Skin"), QDir::homePath(), tr("PNG Image(*.png)"));
QFile file(filename);

file.open(QIODevice::WriteOnly);
file.write(pngData);
file.close();
}

void MainWindow::on_clear_clicked()
{
pngData.clear();
ui->preview->clear();
ui->preview->setStyleSheet("background: transparent ;");
ui->lineEdit->clear();
}

void MainWindow::on_lineEdit_returnPressed()
{
on_dLoad_clicked();
}


Added after 47 minutes:

Alright so I may have found a way around it, but I'm not sure if it would work since I don't have a Linux computer.


void MainWindow::on_save_clicked()
{
if (pngData.isEmpty())
{
QPixmap noSaveData(":/graphic/noSaveData.png");
ui->preview->setStyleSheet("background: transparent ;");
ui->preview->setPixmap(noSaveData);
return;
}
QString filename = QFileDialog::getSaveFileName(this, tr("Save Skin"), QDir::homePath(), tr("PNG Image(*.png)"));

QFile file(filename);
QFileInfo fi(file);
if(fi.suffix().isEmpty())
{
fi.suffix() = "png";
}

file.open(QIODevice::WriteOnly);
file.write(pngData);
file.close();
}


Would this work?