PDA

View Full Version : Assisgn icon to my files with Qt



SirJonas
12th December 2016, 01:07
Hi. It's possible if i'm joining different's files with qt to assign one icon. My problem is when i join different files in one alone and i change in this alone the icon will be changed in all my files and then some files will be corrupted. In other words, someone know some way to assign one icon to one file. For example i'm using one qfile i can't assign to this one icon?

anda_skoa
12th December 2016, 11:44
Maybe you can explain what you mean with "assign an icon"?

Are you displaying files in your program somehow? Qt view with a QFileSystemModel perhaps?

Cheers,
_

SirJonas
12th December 2016, 14:07
Hi i'm trying to bind different files in one with Qt. But my problem is when i bind all files in one and change the resources of this one (the other's are corrupted). In other words, for example i change the icon in the output file(archivosalida) the other files are corrupted. So i was thinking to include my stub like resource because in my stub i add the other file's(all data) and their resources. So if i add my stub like resource when i change the icon in my single file, the resources of other's will not change. But i'm not sure if i can. I put the source of what things i'm trying.

Binder:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFileDialog>
#include <QFile>
#include <QTextStream>

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect (ui->botonExaminar1,SIGNAL(clicked()),this,SLOT(examina r1()));
connect (ui->botonExaminar2,SIGNAL(clicked()),this,SLOT(examina r2()));
connect (ui->botonUnir,SIGNAL(clicked()),this,SLOT(juntar()));
}

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

void MainWindow::examinar1()
{
ui->ejecutable1Texto->setText(QFileDialog::getOpenFileName(this,"Abrir archivo"));
}
void MainWindow::examinar2()
{
ui->ejecutable2Texto->setText(QFileDialog::getOpenFileName(this,"Abrir archivo"));
}
void MainWindow::juntar()
{
/** función para juntar los dos ejecutables **/

/* declaraciones */
QFile archivoSalida;
QFile *archivoEjecutable1;
QFile *archivoEjecutable2;
QFile *archivoStub;
QByteArray tamano1;
QByteArray tamano2;
QByteArray tamano3;
QByteArray trama(1024,'0');
QString nombreEjecutable1;
QString nombreEjecutable2;

/* inicializaciones */
archivoEjecutable1 = new QFile();
archivoEjecutable2 = new QFile();
archivoStub = new QFile();

/* establecer nombres de los ficheros */
archivoSalida.setFileName(QFileDialog::getSaveFile Name(this,"Archivo de salida"));
archivoEjecutable1->setFileName(ui->ejecutable1Texto->text());
archivoEjecutable2->setFileName(ui->ejecutable2Texto->text());
archivoStub->setFileName("./stubb.dat");

/* abrir ficheros */
archivoSalida.open(QFile::WriteOnly);
archivoEjecutable1->open(QFile::ReadOnly);
archivoEjecutable2->open(QFile::ReadOnly);
archivoStub->open(QFile::ReadOnly);

/* escribir en el fichero de salida los tres ejecutables */
archivoSalida.write(archivoStub->readAll() + archivoEjecutable1->readAll() + archivoEjecutable2->readAll());

/* Convertir los tamaños a QString */
tamano1.setNum(archivoStub->size());
tamano2.setNum(archivoEjecutable1->size());
tamano3.setNum(archivoEjecutable2->size());

/* Cojer los nombres de los ejecutables */
nombreEjecutable1 = archivoEjecutable1->fileName().split(QDir::separator()).last();
nombreEjecutable2 = archivoEjecutable2->fileName().split(QDir::separator()).last();

/* Crear la trama de datos */
trama = tamano1 + "|@|" + tamano2 + "|@|" + tamano3 + "|@|" + nombreEjecutable1.toLatin1() + "|@|" + nombreEjecutable2.toLatin1();
/* Escribir la trama en los últimos 1024 bytes del archivo de salida */
archivoSalida.write(trama,1024);

/* Cerrar todos los ficheros */
archivoEjecutable1->close();
archivoEjecutable2->close();
archivoStub->close();
}


My Stub to run my other files in one.

#include "widget.h"
#include "ui_widget.h"
#include <QFile>
#include <QDir>
#include <QProcess>

Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
QDir directorio;
QFile *archivoStub;
QFile *archivoEjecutable1;
QFile *archivoEjecutable2;
QString trama;
QString tamano1;
QString tamano2;
QString tamano3;
QString nombre1;
QString nombre2;
archivoStub = new QFile();
archivoEjecutable1 = new QFile();
archivoEjecutable2 = new QFile();
archivoStub->setFileName(QApplication::applicationFilePath());
QFileInfo info1(QApplication::applicationFilePath());
QString fileName=info1.fileName();
archivoStub->open(QFile::ReadOnly);
archivoStub->seek(archivoStub->size() - 1024);
trama = archivoStub->read(1024);
tamano1 = trama.split("|@|")[0];
tamano2 = trama.split("|@|")[1];
tamano3 = trama.split("|@|")[2];
nombre1 = trama.split("|@|")[3];
nombre2 = trama.split("|@|")[4];

archivoEjecutable1->setFileName(directorio.tempPath() + "/" + nombre1);
archivoEjecutable1->open(QFile::WriteOnly);
archivoStub->seek(tamano1.toInt());
archivoEjecutable1->write(archivoStub->read(tamano2.toInt()));
archivoEjecutable1->close();
QProcess::startDetached(directorio.tempPath() + "/" + nombre1);

archivoEjecutable2->setFileName(directorio.tempPath() + "/" + nombre2);
archivoEjecutable2->open(QFile::WriteOnly);
archivoStub->seek(tamano1.toInt() + tamano2.toInt());
archivoEjecutable2->write(archivoStub->read(tamano3.toInt()));
archivoEjecutable2->close();
QProcess::startDetached(directorio.tempPath() + "/" + nombre2);
QProcess process;
process.start("taskkill /f /im "+fileName);
process.waitForFinished();
process.waitForReadyRead();
qDebug("test");
QCoreApplication::quit();
}

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


My question how i can avoid that i change resources of my other's file or my stub to let me change the resources in my single file( the file with all my application's).

anda_skoa
13th December 2016, 12:02
I have still no idea what you are asking for, but your code is obviously wrong.

You read 1024 byte from the end but your writing never ensures it writes 1024 bytes.

Any specific reason you are not using a standard archive format, e.g. ZIP?

Cheers,
_

SirJonas
13th December 2016, 20:05
ok i change that:


QByteArray trama(1024,'0'); -> QByteArray trama;
archivoSalida.write(trama,1024); -> archivoSalida.write(trama):

But anyways i want to assign some icon to my file ArchivoSalida or some way that my other exe files will not be affected when i change the icon for example of my file archivosalida ( archivosalida is the file that contain all my exe files) And if i change the icon in this file, change me the resources in all my files and will be corrupted. So i was trying some way to avoid to change the resource of these files.

My questions are:
The first thing: i can include a resource file at the content of my file. And if this could be useful to assign one icon to my file archivosalida.
Second: If it is not possible i can put my executables files like resources to avoid that they are corrupted.
The problem is this:
When i get file ArchivoSalidam and i change the icon of this file, the all files inside of archivosalida will change their resources and many will be corrupted. So someway to avoid that with code?¿ or not possible.:confused::confused::confused:

Added after 1 15 minutes:

If i change this:
QByteArray trama(1024,'0'); -> QByteArray trama;

I get this error:
12246

the strange thing i can join files of 100 MB or without any limit using one trama of 1024 bytes¿? LOL

d_stranz
13th December 2016, 20:11
Ordinary files do not have "resources" and don't have icons. Your operating system (Windows, I assume) associates an icon with a file extension, and will display this icon in Explorer next to each file with the same extension. For example, it displays a "document" icon next to the name of every Microsoft Word document that has the .doc, .docx, etc.

Executable files (.exe) can have an icon resource linked into them when the .exe file is created. Explorer will display this icon in the file list and will use it for the desktop link when you create a shortcut.

Your "archivoSalida" file is not an executable file; neither are your "archivoEjecutable" files. They are ordinary binary files and do not contain either "resources" or "icons".


When i get file ArchivoSalidam and i change the icon of this file, the all files inside of archivosalida will change their resources and many will be corrupted.

The code you have posted contains no references to icons at all, so I have no idea what you are actually doing when you say you "change the icon of this file".

SirJonas
16th December 2016, 01:33
You read 1024 byte from the end but your writing never ensures it writes 1024 bytes.

One question about this. in trama i'm writting this:
trama = tamano1 + "|@|" + tamano2 + "|@|" + tamano3 + "|@|" + nombreEjecutable1.toLatin1() + "|@|" + nombreEjecutable2.toLatin1();

This contains different strings maybe can exceed more than 1024 bytes¿??? one string can be more weighty than 1024 bytes?¿

How you recommend me include the size of my trama and my trama to be read for my stub. I can use qdatastream but like i'm using qfile seek i dont know if i could do both things. Anyways i think with qdatastream i can repeat the same thing that i'm trying to do and include the size of my trama.

Added after 1 32 minutes:

The more strange things the program only works when i compile since linux with gcc of linux. If i try with mingw of windows or something dont work.

Added after 5 minutes:

when i say How you recommend i'm trying to say if you know other way. But yeah i understand what you say. But in my string i only include the number's of my different files and names. I dont know if this can exceed more than 1024 bytes probably yes. Anyways i know more ways. But like i said before i dont know why the stuff works fine when i compile my binder in linux stub can be compiled in any operating system. But when i compile my binder in windows the program doesnt seem's to work.

anda_skoa
16th December 2016, 10:51
You could write the meta data to the beginning of the file, when you also know how long the meta data itself is.

But really, not just use a standard container format like ZIP and get everything for free?

Cheers,
_

SirJonas
16th December 2016, 12:51
i'm trying to do a easy binder to join different files not for compressing. If i use zip i'm compressing my files. I can do once time and i get one single file. This is i'm trying. This is easy example using qfile seek but i dont know if it is better to use qdatastream and order the data and then read this data i dont know. Anyways seem's not works the file when i bind however in linux works so i think the code not works fine because i tried the compilation in linux and when i run the file works like i did. But in windows when i compile not seem's to work. I dont know if i do cross-compiling will be useful. But i think it's not necessary this is a small application basic. So my first question was that if it is better to use qdatastream to order the content and then read it and make all like i'm doing now. And the other about this strange behaviour only works when i compile the program in linux.

The first difference i'm seeing with linux i must to put absolute path for example if i have my stub in the same place of my program and i use relative path show me:

QIODevice::read (QFile, "stubb.exe"): device not open

Code:

archivoStub->setFileName("stubb.exe");

So no problem i put the absolute path. But when i run the program bind files but doesnt seek the files and this doesnt do nothing. However if i compile my binder in linux bind files correctly and works in windows.
And i think because some bug o error in windows for this the program doesnt seems to work in windows.
The bug i'm refering is this:
https://bugreports.qt.io/browse/QTBUG-52618

In windows 7 doesnt show the message. But in windows 10 show me this message. Anyways when i bind files with compilation of linux works and when i do with windows doesnt work too? So i think is that.

For this i was little confused.

anda_skoa
16th December 2016, 14:13
i'm trying to do a easy binder to join different files not for compressing. If i use zip i'm compressing my files.

No, why?
What you would get is an easy way to combine multiple files into one file.



I can do once time and i get one single file.

Which is what an archive format would do.



This is i'm trying.

The questions is why you are trying it yourself instead of just using a library for archives?

Cheers,
_

SirJonas
16th December 2016, 15:57
Because i saw open project of binders in perl, java, c++, python. But no one in Qt so i was trying to do one in Qt. and Why ¿? Because i have different applications and i want to run at the same time with .exe format not compressed file like anyone. But if you ask me about personal reason because i want to do open source binder/joiner of any type of file however my progress so bad like you can see. Anyways, i think is a bug for windows because when i compile my binder in windows and i run the application never seeks my files and etc.... But when i do in linux bind the files with compilation in linux bind perfectly and run fine. So or i'm idiot or something strange happen. So anyways with cross-compiling creating .exe since linux works but it's like a big shit because you must to do cross-compiling to make one easy ugly binder. So can be solved with cross-compiling and i get run my binder in windows but i can't say why my binder must to be compiled in linux to get that the program works and why when i compile in windows the program seem's not to work.:confused::confused: i leave so then.

But the program works fine only you must to compile the binder in linux. And i can't say why happen this. It's very very strange.
And the other thing i must to take care with the size of my string can exceed 1024 bytes but i'm not inserting a big data to be a trouble. Anyways i can put one limit to avoid this problem. Because using qdatastream i dont know if it is a good idea. i leave so then.

anda_skoa
17th December 2016, 11:54
Because i saw open project of binders in perl, java, c++, python. But no one in Qt so i was trying to do one in Qt.

I have no idea what "binders" are or why one would use some obscure custom format instead of a standard archive format, but if there is a C++ implementation then it is directly usabkle in a Qt program as Qt is a C++ framework.



Because i have different applications and i want to run at the same time with .exe format not compressed file like anyone.

I am not talking about compression, a ZP file, for example, doesn't need to use any compression.
There are other archive formats that don't even have compression at all, e.g. Tar.



But if you ask me about personal reason because i want to do open source binder/joiner of any type of file however my progress so bad like you can see.

Ok, so this is a personal training exercise, you want to learn file handling and chose creating a custom archive format as your target.
Fair enough.



So anyways with cross-compiling creating .exe since linux works but it's like a big shit because you must to do cross-compiling to make one easy ugly binder. So can be solved with cross-compiling and i get run my binder in windows but i can't say why my binder must to be compiled in linux to get that the program works and why when i compile in windows the program seem's not to work.:confused::confused: i leave so then.

I doubt cross-compiling will change anything, if your program misbehaves during runtime.



But the program works fine only you must to compile the binder in linux. And i can't say why happen this. It's very very strange.

That's indeed very strange.



And the other thing i must to take care with the size of my string can exceed 1024 bytes but i'm not inserting a big data to be a trouble.

If you write your metadata at the beginning of the result file you won't have any restriction of its size.
E.g. by first writing the length of the meta data.



Because using qdatastream

That's also an option for having no limit, the stream will write the length of the string in a similar fashion.

Cheers,
_

SirJonas
17th December 2016, 13:35
I have no idea what "binders" are or why one would use some obscure custom format instead of a standard archive format, but if there is a C++ implementation then it is directly usabkle in a Qt program as Qt is a C++ framework.
-->
peoples mostly using file splitter for split file that will be uploaded to hosting servers, because some hosting server have their maximum upload file size, this program can merge HJSplit’s splitted files.

anda_skoa
17th December 2016, 16:48
If you are restoring a single file that has been splt into pieces, you don't need to bother with any additional data.
Just read each partial file and write to the target in the correct order.

Cheers,
_

SirJonas
18th December 2016, 00:40
ok i know why doesnt work because i'm retard only for that. now i'm checking one solution to get my trama without limiting the space of my string.
when you say: If you write your metadata at the beginning of the result file you won't have any restriction of its size.

The problem i must to know the size of my trama to extract it. But if i dont know maybe i get some qassert or list out of range. So i was checking qdatastream or something like that for this reason. Anyways, i'm checking this you said me:

If you write your metadata at the beginning of the result file you won't have any restriction of its size.
E.g. by first writing the length of the meta data.

And checking too qdatastream


That's also an option for having no limit, the stream will write the length of the string in a similar fashion.

But at the momenti didnt get a minimal example how would be. I'm trying.

anda_skoa
18th December 2016, 13:01
[QUOTE=SirJonas;296208]The problem i must to know the size of my trama to extract it.
QString has a method to get its length, QByteArray as well.

When using a QDataStream it will also first write the length of the string, then the string, so that it know how far to read when reading the string.

Cheers,
_

SirJonas
18th December 2016, 13:45
yeah i can put the size of my string but after how i will read it? The only way is using qdatastream? Include me the content at first or at the end of my file?
I proved but i get this error:

1297780736
QIODevice::read (QFile, "\Desktop\visual\build-binder-Qt_5_7_0_msvc2
013_static-Debug\debug\sha.exe"): maxSize argument exceeds QByteArray size limit

ASSERT failure in QList<T>::operator[]: "index out of range", file C:\Qt\Qt5.7.0
\5.7\mingw53_32\include/QtCore/qlist.h, line 545

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.

anda_skoa
18th December 2016, 20:32
yeah i can put the size of my string but after how i will read it? The only way is using qdatastream?

QDataStream is the easiest option.

Alternatively using QFile::read()



Include me the content at first or at the end of my file?

It is easier at the beginning since you can just read, if you add your meta data to the end you will first need to determine the length, then jum ahead, then read the meta data, then just to beginning and start reading the content.

Cheers,
_