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. Problem with Ui::Dialog not recognized
QtWS25 Last Chance

Problem with Ui::Dialog not recognized

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 5 Posters 253 Views
  • 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.
  • A Offline
    A Offline
    auvonspam
    wrote on 1 Apr 2025, 17:06 last edited by
    #1

    I am following the Qt Shared Memory example here: https://doc.qt.io/qt-5/qtcore-ipc-sharedmemory-example.html

    I am having an issue replicating the code. When I declare Ui::Dialog ui as a private variable, the compiler throws an error: "invalid use of incomplete type 'class Ui'". It's saying it does not have a type. So basically, it doesn't recognize Ui::Dialog as a valid type :(

    Things tried:

    1. Including dialog.h.
    2. Making Ui::Dialog a pointer.

    Here is my code:

    // dialog.h
    #ifndef DIALOG_H
    #define DIALOG_H
    
    #include <QApplication>
    #include <QSharedMemory>
    #include <QLabel>
    #include <QPushButton>
    #include <QVBoxLayout>
    #include <QFileDialog>
    #include <QImage>
    #include <QBuffer>
    #include <QDataStream>
    #include <QDebug>
    // #include "dialog.h"
    
    class SharedMemoryDialog : public QDialog
    {
        Q_OBJECT
    
    public:
        SharedMemoryDialog(QWidget *parent = nullptr)
            : QDialog(parent)
            , shmKey("ShmemKey")
            , sharedMemory(shmKey, 0)
        {
            setupUI();
        }
    
    private slots:
        void loadFromFile()
        {
            // 1. Create sample data
            QString data = "\nHello from shared memory!";
            
            // 2. Create a buffer and stream
            QBuffer buffer;
            buffer.open(QBuffer::ReadWrite);
            QDataStream out(&buffer);
            out << data;
            
            // 3. Create shared memory segment
            int size = buffer.size();
            if (!sharedMemory.create(size)) {
                if (sharedMemory.error() == QSharedMemory::AlreadyExists) {
                    sharedMemory.attach();
                } else {
                    qDebug() << "Unable to create shared memory segment:" 
                             << sharedMemory.errorString();
                    return;
                }
            }
            
            // 4. Copy data to shared memory
            sharedMemory.lock();
            char *to = (char*)sharedMemory.data();
            const char *from = buffer.data().data();
            memcpy(to, from, qMin(sharedMemory.size(), size));
            sharedMemory.unlock();
            
            qDebug() << "Image written to shared memory";
        }
    
        void loadFromMemory()
        {
            // If not attached, try to attach and if that fails, error out
            if (!sharedMemory.isAttached() && !sharedMemory.attach()) {
                switch (sharedMemory.error()) {
                case QSharedMemory::NotFound:
                    qDebug() << "Shared memory not found";
                    break;
                case QSharedMemory::PermissionDenied:
                    qDebug() << "Permission denied";
                    break;
                case QSharedMemory::InvalidSize:
                    qDebug() << "Invalid size";
                    break;
                case QSharedMemory::AlreadyExists:
                    qDebug() << "Already exists";
                    break;
                default:
                    qDebug() << "Unknown error:" << sharedMemory.errorString();
                }
                return;
            }
            
            // Read data from shared memory.
            sharedMemory.lock();
            QBuffer buffer;
            buffer.setData((char*)sharedMemory.constData(), sharedMemory.size());
            buffer.open(QBuffer::ReadOnly);
            QDataStream in(&buffer);
            
            QString data;
            in >> data;
            
            sharedMemory.unlock();
            sharedMemory.detach();
            
            qDebug() << "Data read from shared memory:" << data;
        }
    
    private:
        void detach();
    
        void setupUI()
        {
            QVBoxLayout *layout = new QVBoxLayout(this);
            
            QPushButton *writeButton = new QPushButton("Load Image From File...");
            QPushButton *readButton = new QPushButton("Display Image From Shared Memory");
            
            layout->addWidget(writeButton);
            layout->addWidget(readButton);
            
            connect(writeButton, &QPushButton::clicked, this, &SharedMemoryDialog::loadFromFile);
            connect(readButton, &QPushButton::clicked, this, &SharedMemoryDialog::loadFromMemory);
            
            setWindowTitle("Shared Memory Example");
        }
    
        Ui::Dialog ui;
        QString shmKey;
        QSharedMemory sharedMemory;
    };
    
    #endif // DIALOG_H
    
    int main(int argc, char *argv[])
    {
        QApplication app(argc, argv);
    
        SharedMemoryDialog dialog;
        dialog.show();
        
        return app.exec();
    }
    
    #include "main.moc"
    
    1 Reply Last reply
    0
    • C Online
      C Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 1 Apr 2025, 17:55 last edited by
      #2

      What are you trying to achieve? from what it looks in your setupUI() function you don't use Ui::Dialog at all.

      The full example code can be found here (as written in the documentation): https://code.qt.io/cgit/qt/qtbase.git/tree/examples/corelib/ipc/sharedmemory?h=5.15

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      0
      • A Offline
        A Offline
        auvonspam
        wrote on 3 Apr 2025, 16:14 last edited by auvonspam 4 Mar 2025, 16:29
        #3

        I'm trying to follow the full example code: https://doc.qt.io/qt-5/qtcore-ipc-sharedmemory-example.html

        Well just copying and pasting those files into my project worked. The tutorial isn't very clear. I was missing files that were in that repo.

        1 Reply Last reply
        0
        • A Offline
          A Offline
          auvonspam
          wrote on 3 Apr 2025, 16:35 last edited by
          #4

          Thanks for helping me fix that... New to Qt.

          Out of curiosity, how come in that SharedMemory demo, only the 1st instance of the app is able to load data into the QSharedMemory? The instructions say the 1st one should load the data. And it does. But why not the 2nd one, too? Why isn't the ordering interchangeable?

          And what is the meaning behind the // [0] and // [1] comments in the code?

          JonBJ 1 Reply Last reply 7 Apr 2025, 15:17
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on 3 Apr 2025, 19:54 last edited by
            #5

            Hi,

            The instructions says to click the button on one of the dialog so you can use either.

            As for the funny comments, these are used by qdoc to integrate the code surrounded by them in the documentation.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            1
            • A Offline
              A Offline
              auvonspam
              wrote on 7 Apr 2025, 14:30 last edited by
              #6

              But the ordering seems to matter... That's what I don't quite get. If I try to use the 2nd instance that I opened to load data into shmem, it throws an error in the GUI. Why does ordering matter if both instances are identical?

              Pl45m4P 1 Reply Last reply 7 Apr 2025, 14:49
              0
              • A auvonspam
                7 Apr 2025, 14:30

                But the ordering seems to matter... That's what I don't quite get. If I try to use the 2nd instance that I opened to load data into shmem, it throws an error in the GUI. Why does ordering matter if both instances are identical?

                Pl45m4P Offline
                Pl45m4P Offline
                Pl45m4
                wrote on 7 Apr 2025, 14:49 last edited by
                #7

                @auvonspam said in Problem with Ui::Dialog not recognized:

                Why does ordering matter if both instances are identical?

                Your code or the example?
                In the example it should not matter.


                If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                ~E. W. Dijkstra

                1 Reply Last reply
                2
                • A auvonspam
                  3 Apr 2025, 16:35

                  Thanks for helping me fix that... New to Qt.

                  Out of curiosity, how come in that SharedMemory demo, only the 1st instance of the app is able to load data into the QSharedMemory? The instructions say the 1st one should load the data. And it does. But why not the 2nd one, too? Why isn't the ordering interchangeable?

                  And what is the meaning behind the // [0] and // [1] comments in the code?

                  JonBJ Online
                  JonBJ Online
                  JonB
                  wrote on 7 Apr 2025, 15:17 last edited by
                  #8

                  @auvonspam said in Problem with Ui::Dialog not recognized:

                  The instructions say the 1st one should load the data. And it does. But why not the 2nd one, too?

                  You are aware that in the first instance you push a button to tell it to load the data from file and push to shared memory while in the second instance you push a different button telling it to read from the shared memory? As for which of the two dialogs you click first or second that should not matter. Just you need to load from file to memory before you try to read from memory.

                  1 Reply Last reply
                  1

                  1/8

                  1 Apr 2025, 17:06

                  • Login

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