Slot and Qbytearray
-
Is it possible to define a slot qbytearray instead of void like QBytearray on_load_image() so that it can return a qbytearray
-
Well yes, but return it to where? When you make a
connect
nobody is expecting a return value and nobody would consume it.
In design terms slots are listeners (or callbacks or whatever you call that) that modify object state when invoked. They are not meant as a value factories. -
@Chris-Kawa
I have 2 private slots. One to load a picture and the second one to save picture and details in my database.
Load picture
@fileName = QFileDialog::getOpenFileName(this,
"Select image",
".",
"File (*.png)");
ui->imageDisplay3->setPixmap(QPixmap(fileName));
//QFileInfo fi(fileName);
QApplication::processEvents();
this->adjustSize();
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly)) return;
QByteArray byteArray = file.readAll();
@
I want to return byteArray so I could save it in the second slot -
Not every member function needs to be a slot. If it produces some value it doesn't sound very slottish.
Consider you have a slot like
QByteArray MyClass::saveData()
and you connect it to some button:
connect(button, &QPushButton::clicked, myObject, &MyClass::saveData)
. You can connect it that way but where would the byte array data go? Button is certainly not gonna do anything with it.Instead something like this seems more reasonable:
class MyClass { ... public slots: //could be private or whatever void processPictureData() { savePictureData(loadPictureData()); } void savePictureData(const QByteArray& data) { /* write the data to some storage thing place */ } public: //could be private or whatever, but it's not a slot QByteArray loadPictureData() { // it could even be made static member to allow greater reusability /* do what you did and return the data */ } }
Now you have a nice modular design, each piece of functionality reusable in isolation and hermetic.
You can connect either of the slots to some signal.Btw. as the help in the footer suggests please use ``` around your code, not @.