Cannot save the contents of a QGraphicsScene in QGraphicsView - qt

I want my application to save the contents of my scene which contains graphics items in it and later be able to retrieve all those items(not as a QPixmap, but as actual individual items).
I have tried using Qsettings but get a QVariant error.
My code is:
QSettings settings("AdvProgLab","ERapp");
settings.beginGroup("MainWindow");
settings.setValue("saved_scene",scene); //Here, scene is a QGraphicsScene
settings.endGroup();
And the error which I am getting is:
qvariant.h:471: error: ‘QVariant::QVariant(void*)’ is private
inline QVariant(void *) Q_DECL_EQ_DELETE;
^
error: use of deleted function ‘QVariant::QVariant(void*)’
Can anybody please explain me where I am going wrong ?
Or is there any other way by which I can achieve this .
Thank You.

Related

Why show popup function of combobox is not working in qt?

I am new to qt and just experimenting with some functions of comboBox.
I have written the following code
ui->comboBox->addItem("mark");
ui->comboBox->count();
ui->showPopup();
in main window() of project
this code has added mark to the comboBox if i write the following code
ui->comboBox->addItem("mark");
but count() and showPopup() are not working and following error is coming.
class Ui::MainWindow has no member named show Popup.
as you can see in the qt doc here https://doc.qt.io/qt-5/qcombobox.html#showPopup
showPopup is a method defined in the combobox and not in the mainWindows class
so you need to do
ui->comboBox->showPopup();
instead of:
ui->showPopup();
on the other hand, the function count() defined here https://doc.qt.io/qt-5/qcombobox.html#count-prop
is actually returning the number of items in the combobox
so when you do
ui->comboBox->count();
and ignore the returned value, well.. it just get lost in the app...
try comething like
qDebug() << "Elements in CB: " << ui->comboBox->count();
so you can printed in the terminal as a debug message...

Use an QObject found by it's QObjectName

I have a little problem in my program. I have a config file put in settings. I pull from it the names of the object I need to be checked (these are QCheckBox).
I have this piece of code (It compiles and runs but when it's at "cBox->setChecked" it just crash):
void Preproc::on_tBtnManual_toggled(bool checked){
if(checked){
ui->tBtnManual->setText("Systematic");
}else{
ui->tBtnManual->setText("Manual");
settings.beginGroup("Preprocessing");
QStringList keys = settings.childKeys();
foreach(QString configParam,keys){
QCheckBox *cBox = ui->gridLayout->findChild<QCheckBox *>(configParam);
cBox->setChecked(settings.value(configParam).toBool());
}
}
}
I have tried to put ui->cBox->... put it says that cBox is not a child of ui.
If I qDebug(cBox) I have a QObject(0x0) so nothing !
I'm a little new to Qt so maybe it's a simple thing.
Thanks and have a nice day :)
Are you sure that an object is found?
I don't think so (different name? wrong layout?). cBox is 0x0 when nothing is found.
However put a
if (cBox)
before
cBox->setChecked(settings.value(configParam).toBool());
and it will not crash anymore when it doesn't find an object by name.
are you sure the name (content of configParam) is correct?
you can try the search from QApplication
QApplication::instance()->findChild<QCheckBox *>(configParam);
the findChild method performs a recursive search, if the object exists in the hirachie, it will be found. if the object is not found, it could be:
the object does not exist
the object has another name
the object or one of its ancestors has no (NULL) parent
can you post the part of the .ui file with the check box? it would be helpful.

qt malloc(): smallbin double linked list corrupted

In my Qt widget I sometimes get this error:
malloc(): smallbin double linked list corrupted
It does not happen all the time but I think I have narrowed it down to when it starts.
I have a QGraphicsView and QGraphicsScene and there I'm drawing lines whos points are stored in a vector. Reason for this is I need to pass this points to another library. Once I draw the points I have an option if I click on a line I'm prompted to another window where I can change the coordinates of a line.
ResizeDialog *dialog = new ResizeDialog(this);
dialog->exec();
delete dialog;
The above code is the code I use to open a new QDialog. I know if I use this->close() the
qt malloc(): smallbin double linked list corrupted does not appear but then I lose the instance of QGraphicsView. Reason I need to keep the QGraphicsView window open if I need to chose to add further lines.
Any advice on how I can eliminate this issue wold be helpful.
Rather than using delete dialog;, use dialog->deleteLater();. I assume the small code portion is inside a slot of the object referenced by "this", and direct deletion is source of trouble as ResizeDialog *dialog = new ResizeDialog(this); affect the parent object this.

QGraphicsScene : get the QGraphicsItemGroup under the cursor

I'm having trouble getting a pointer of a QGraphicsItemGroup bellow my cursor. QGraphicsScene::ItemAt return one of the objects of the group but the not ItemGroup it self.
Any clue how to do it ?
Thx
You can call QGraphicsItem::group() on the QGraphicsItem returned by QGraphicsScene::itemAt().

QPlainTextEdit segmentation fault

I have some Qt application with QPlainTextEdit in Tab widget. When try to make a pointer on it
QPlainTextEdit *w = (QPlainTextEdit*)ui->tabWidget->widget(0)
and call a document() method
w->document()
I get a segfault.
But if i call document directly, e.g. ui->mainEdit->document(), then everything works fine.
Can anybody explain me why it happens?
You want to do:
QPlainTextEdit *w = ui->mainEdit;
Then w->document() will return what you want. You are getting the segmentation fault because when you cast ui->tabWidget->widget(0); gives a pointer to a tab page object. When you cast this to QPlainTextEdit* are telling your program to treat a part of memory that does not represent a QPlainTextEdit as a QPlainTextEdit. This causes trouble at the time that you call w->document() because that is in the memory location that it tries to access is not what it would expect from memory which belongs to QPlainTextEdit.
i'm almost sure, that ui->tabWidget->widget(0) return container widget inside of tabWidget. Try qDebug() << ui->tabWidget->widget(0)->metaObject()->className() and see what is printed. It's probably just "QWidget" not "QPlainTextEdit". Your edit is inside of layout of this widget
You can use qobject_cast to make sure that it returns the right type.
QPlainTextEdit *w = qobject_cast<QPlainTextEdit*>(ui->tabWidget->widget(0));
if (w)
{
...
}
It'll return 0 if the type is not of QPlainTextEdit*.
As stated, widget(0) is probably not returning what you wanted - and probably contains a container or some other item, and is probably not the way you want to be accessing your widgets unless there is no other way.

Resources