Results 1 to 5 of 5

Thread: [Qt5] Set Default Suffix

  1. #1
    Join Date
    Jan 2013
    Location
    Temecula, CA
    Posts
    19
    Thanks
    11
    Qt products
    Qt5
    Platforms
    Windows

    Default [Qt5] Set Default Suffix

    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:
    Qt Code:
    1. void MainWindow::on_save_clicked()
    2. {
    3. if (pngData.isEmpty())
    4. {
    5. QPixmap noSaveData(":/graphic/noSaveData.png");
    6. ui->preview->setStyleSheet("background: transparent ;");
    7. ui->preview->setPixmap(noSaveData);
    8. return;
    9. }
    10. QString filename = QFileDialog::getSaveFileName(this, ("Save Skin"), "C:/Users/", ("PNG Image (*.png)"));
    11. QFile file(filename);
    12. file.open(QIODevice::WriteOnly);
    13. file.write(pngData);
    14. file.close();
    15. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    May 2008
    Location
    Tripoli Libya
    Posts
    70
    Thanks
    10
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: [Qt5] Set Default Suffix

    Qt Code:
    1. QFileDialog::getSaveFileName(0,"Save Skin",QDir::homePath(),"PNG Image (*.png)");
    To copy to clipboard, switch view to plain text mode 
    there is no C:/Users in Linux ...read The Current Directory and Other Special Paths
    http://doc.qt.io/qt-4.8/QDir
    Last edited by alrawab; 14th February 2013 at 04:47.

  3. The following user says thank you to alrawab for this useful post:

    Salads (14th February 2013)

  4. #3
    Join Date
    Jan 2013
    Location
    Temecula, CA
    Posts
    19
    Thanks
    11
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: [Qt5] Set Default Suffix

    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:
    output.jpg
    Qt Code:
    1. QFSFileEngine::open: No file name specified
    2. Cannot create accessible interface for object: MainWindow(0x33fbd4, name = "MainWindow")
    3. Cannot create accessible interface for object: QPushButton(0x8f4880, name = "save")
    4. Cannot create accessible interface for object: MainWindow(0x33fbd4, name = "MainWindow")
    5. Cannot create accessible interface for object: MainWindow(0x33fbd4, name = "MainWindow")
    6. Cannot create accessible interface for object: QPushButton(0x8f4880, name = "save")
    7. Cannot create accessible interface for object: QPushButton(0xa77478, name = "dLoad")
    8. Cannot create accessible interface for object: QLineEdit(0xa774d8, name = "lineEdit")
    9. Cannot create accessible interface for object: QPushButton(0x8f4880, name = "save")
    10. Cannot create accessible interface for object: QLabel(0x8f4900, name = "preview")
    11. Cannot create accessible interface for object: QPushButton(0x8f4960, name = "clear")
    12. Cannot create accessible interface for object: QWidget(0xa77418, name = "centralWidget")
    13. Cannot create accessible interface for object: MainWindow(0x33fbd4, name = "MainWindow")
    14. Cannot create accessible interface for object: MainWindow(0x33fbd4, name = "MainWindow")
    To copy to clipboard, switch view to plain text mode 

    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.
    Last edited by Salads; 14th February 2013 at 05:13.

  5. #4
    Join Date
    May 2008
    Location
    Tripoli Libya
    Posts
    70
    Thanks
    10
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: [Qt5] Set Default Suffix

    its works fine if we supposed pngData is correct data type
    Qt Code:
    1. #include <QApplication>
    2. #include <QFileDialog>
    3.  
    4. int main(int argc, char **argv)
    5. {
    6. QApplication app(argc,argv);
    7. QString filename =QFileDialog::getSaveFileName(0,"Save Skin",QDir::homePath(),"PNG Image (*.png)");
    8. QFile file(filename);
    9. file.open(QIODevice::WriteOnly);
    10. file.write("Foo");//change Foo to your data
    11.  
    12. file.close();
    13. return 0;
    14. }
    To copy to clipboard, switch view to plain text mode 
    QPixmap::save
    Last edited by alrawab; 14th February 2013 at 05:29.

  6. #5
    Join Date
    Jan 2013
    Location
    Temecula, CA
    Posts
    19
    Thanks
    11
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: [Qt5] Set Default Suffix


    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:
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include <QNetworkReply>
    6.  
    7. namespace Ui {
    8. class MainWindow;
    9. }
    10.  
    11. class MainWindow : public QMainWindow
    12. {
    13. Q_OBJECT
    14.  
    15. public:
    16. explicit MainWindow(QWidget *parent = 0);
    17. ~MainWindow();
    18. QByteArray pngData;
    19. QNetworkAccessManager *manager;
    20.  
    21.  
    22. private slots:
    23. void on_dLoad_clicked();
    24.  
    25. void replyFinished(QNetworkReply *reply);
    26.  
    27. void on_save_clicked();
    28.  
    29. void on_clear_clicked();
    30.  
    31. void on_lineEdit_returnPressed();
    32.  
    33. private:
    34. Ui::MainWindow *ui;
    35. };
    36.  
    37. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    mainwindow.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QIcon>
    4. #include <QNetworkAccessManager>
    5. #include <QNetworkRequest>
    6. #include <QNetworkReply>
    7. #include <QDebug>
    8. #include <QFileDialog>
    9. #include <QFile>
    10. #include <QDir>
    11.  
    12. MainWindow::MainWindow(QWidget *parent) :
    13. QMainWindow(parent),
    14. ui(new Ui::MainWindow)
    15. {
    16. ui->setupUi(this);
    17. setWindowTitle("Minecraft Skin Downloader v0.7"); //Version
    18. QIcon icon(":/graphic/ico.png");
    19. setWindowIcon(icon);
    20. setFixedSize(400, 200);
    21. setStyleSheet("QMainWindow { background-image: url( :/graphic/bg.png); } ");
    22. setWindowFlags(windowFlags() ^ Qt::WindowMaximizeButtonHint);
    23. ui->lineEdit->setToolTip("Username (Case Sensitive)");
    24.  
    25. manager = new QNetworkAccessManager(this);
    26. connect(manager, SIGNAL(finished(QNetworkReply *)), this, SLOT(replyFinished(QNetworkReply *)));
    27. }
    28.  
    29. MainWindow::~MainWindow()
    30. {
    31. delete ui;
    32. }
    33.  
    34. void MainWindow::on_dLoad_clicked()
    35. {
    36. //s3.amazonaws.com/MinecraftSkins/NICKNAME.png
    37. QString prefix = "http://s3.amazonaws.com/MinecraftSkins/";
    38. QString usrl = prefix += ui->lineEdit->text() += ".png";
    39.  
    40. QUrl url(usrl);
    41. QNetworkRequest request(url);
    42.  
    43. manager->get(request);
    44. }
    45.  
    46. void MainWindow::replyFinished(QNetworkReply *reply)
    47. {
    48. if(reply->error() != QNetworkReply::NoError)
    49. {
    50. qDebug() <<"Error in" << reply->url() << ":" << reply->errorString();
    51. QPixmap noUsr(":/graphic/voidUser.png");
    52. ui->preview->setStyleSheet("background: transparent ;");
    53. ui->preview->setPixmap(noUsr);
    54. pngData.clear();
    55. return;
    56. }
    57.  
    58. pngData = reply->readAll();
    59. QPixmap srcPic;
    60. srcPic.loadFromData(pngData);
    61. QPixmap scaled = srcPic.scaled(128, 64, Qt::KeepAspectRatio, Qt::FastTransformation);
    62. ui->preview->setStyleSheet("QLabel { background-color: white ;}");
    63. ui->preview->setPixmap(scaled);
    64.  
    65. }
    66.  
    67. void MainWindow::on_save_clicked()
    68. {
    69. if (pngData.isEmpty())
    70. {
    71. QPixmap noSaveData(":/graphic/noSaveData.png");
    72. ui->preview->setStyleSheet("background: transparent ;");
    73. ui->preview->setPixmap(noSaveData);
    74. return;
    75. }
    76. QString filename = QFileDialog::getSaveFileName(this, tr("Save Skin"), QDir::homePath(), tr("PNG Image(*.png)"));
    77. QFile file(filename);
    78.  
    79. file.open(QIODevice::WriteOnly);
    80. file.write(pngData);
    81. file.close();
    82. }
    83.  
    84. void MainWindow::on_clear_clicked()
    85. {
    86. pngData.clear();
    87. ui->preview->clear();
    88. ui->preview->setStyleSheet("background: transparent ;");
    89. ui->lineEdit->clear();
    90. }
    91.  
    92. void MainWindow::on_lineEdit_returnPressed()
    93. {
    94. on_dLoad_clicked();
    95. }
    To copy to clipboard, switch view to plain text mode 


    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.
    Qt Code:
    1. void MainWindow::on_save_clicked()
    2. {
    3. if (pngData.isEmpty())
    4. {
    5. QPixmap noSaveData(":/graphic/noSaveData.png");
    6. ui->preview->setStyleSheet("background: transparent ;");
    7. ui->preview->setPixmap(noSaveData);
    8. return;
    9. }
    10. QString filename = QFileDialog::getSaveFileName(this, tr("Save Skin"), QDir::homePath(), tr("PNG Image(*.png)"));
    11.  
    12. QFile file(filename);
    13. QFileInfo fi(file);
    14. if(fi.suffix().isEmpty())
    15. {
    16. fi.suffix() = "png";
    17. }
    18.  
    19. file.open(QIODevice::WriteOnly);
    20. file.write(pngData);
    21. file.close();
    22. }
    To copy to clipboard, switch view to plain text mode 

    Would this work?
    Last edited by Salads; 14th February 2013 at 07:07.

Similar Threads

  1. Qt Creator Exotic C++ Suffix
    By redruM in forum Qt Tools
    Replies: 4
    Last Post: 29th November 2012, 09:23
  2. Replies: 13
    Last Post: 19th June 2011, 21:04
  3. Replies: 0
    Last Post: 17th December 2010, 08:50
  4. suffix for qt dlls
    By jobrandt in forum Installation and Deployment
    Replies: 6
    Last Post: 11th April 2008, 09:59
  5. how to add suffix to library like qtcore4_gcc.dll
    By furk in forum Installation and Deployment
    Replies: 3
    Last Post: 13th June 2007, 15:11

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.