Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Slot and Qbytearray
Forum Updated to NodeBB v4.3 + New Features

Slot and Qbytearray

Scheduled Pinned Locked Moved General and Desktop
slotqbytearray
4 Posts 2 Posters 1.7k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Gillou_beginqt
    wrote on last edited by
    #1

    Is it possible to define a slot qbytearray instead of void like QBytearray on_load_image() so that it can return a qbytearray

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by Chris Kawa
      #2

      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.

      G 1 Reply Last reply
      0
      • Chris KawaC Chris Kawa

        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.

        G Offline
        G Offline
        Gillou_beginqt
        wrote on last edited by
        #3

        @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

        1 Reply Last reply
        0
        • Chris KawaC Offline
          Chris KawaC Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by Chris Kawa
          #4

          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 @.

          1 Reply Last reply
          1

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved