Problem with Ui::Dialog not recognized
-
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:
- Including dialog.h.
- 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"
-
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
-
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.
-
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? -
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.
-
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?
-
@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. -
@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.
2/8