Why show popup function of combobox is not working in qt? - 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...

Related

Cannot save the contents of a QGraphicsScene in QGraphicsView

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.

Calling QStandardItem mimeData() method

In my app I have a QTableView and a QTreeView. I need to drag from the table view and drop in the tree view. I had this working, but I reworked the app so that the GUI elements were created in C++ and this this has stopped working.
I can pick up an item from the table and drop it on the tree - all looks fine. However, the table model's mimeData() method is not called, so the dropped data is incomplete.
How do I get the drag drop operation to call the mimeData() method?
The tables' model is based on a QStandardItemModel.
Had a similar issue with my code. Make sure you override the mimeData properly. For example, some examples show the prototype as:
QMimeData *mimeData(const QList<QTreeWidgetItem *> &items) const;
while the correct prototype is:
QMimeData * mimeData(const QList<QTreeWidgetItem *> items) const;
(note the missing reference in front of items). And if you implement it incorrectly, you're not overriding the mimeData() but implementing another mimeData() function.
An easy check to make sure you're doing it right is to declare a function you want to override, but change its return type from QMimeData* to bool, such as:
bool mimeData(const QList<QTreeWidgetItem *> items) const;
If you are overriding a correct function, your code will not compile due to conflicting return type (you're not allowed to override only the return type). If your code compiles fine, you are not overriding anything but instead declaring a new function. Check the function signature.

QT: Getting text content from web page

I've been trying to start with a simple app that retrieves data from a simple HTML page upon clicking a button and stumbled upon a rather helpful tutorial on QT-Project and have been trying to implement it for my own project.
Everything manages to compile until I try to actually try to implement the loadImage function (as found in the tutorial). (I actually had to initialize m_pImgCtrl as Filedownloader * m_pImgCtrl = new FileDownloader(imageUrl, this); and I'm not exactly sure how it's suppose to work without prior object declaration?)
From what I get, m_pImgCtrl isn't actually defined in the loadImage() function since it is initialized outside of the function? Or does the connect() function do something that I'm not too aware of?
Thanks for the help!
The tutorial doesn't tell you the whole story.
The code in section Usage is supposed to be part of a class MainWindow – the controller of your main window (see line 1 of tutorial's last snippet). This class contains a slot loadImaged() called when the NetworkReply has finished. It also has a member FileDownloader * m_pImgCtrl.
For instance, the second Usage snippet could be part of a slot MainWindow::buttonClicked() like
void MainWindow::buttonClicked()
{
QUrl imageUrl("http://qt.digia.com/Documents/1/QtLogo.png");
m_pImgCtrl = new FileDownloader(imageUrl, this);
connect(m_pImgCtrl, SIGNAL(downloaded()), SLOT(loadImage()));
}

QObject::findChild returns null, can't find cross QML objects

I have a question about accessing QML components on the C++ side, sorry it might be little beginner.
I am currently working with the sample map view, the app alone works great, but after I try to put this sample together with a navigation panel, it stopped working.
I have added my own main.qml, another page called menu.qml, and renamed the app's original main.qml to map.qml. So now user would require to navigate the panel like this: main.qml -> menu.qml -> map.qml
My problem is:
Originally, from the sample, app constructor saves a mapView pointer when it creates main.qml so later it can add markers/change location to the mapview.
It gets mapView pointer by calling:
QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(this);
qml->setContextProperty("_mapViewTest", this);
AbstractPane *root = qml->createRootObject<AbstractPane>();
QObject* mapViewAsQObject = root->findChild<QObject*>(QString("mapViewObj"));
mapView = qobject_cast<bb::cascades::maps::MapView*>(mapViewAsQObject);
However, I have changed the name to map.qml, findChild returned as null. Since my app is unable to find mapView, the whole app crushes when I try to set a marker/locate myself.
I tried to QmlDocument::create("asset:///map.qml"), and use the same method as above to find the mapview, and I was able to get it, but nothing happens when I try to put down a marker/locate.
I have also tried to add a new function that calls findChild and get the mapView pointer. The function gets called in onCreationCompleted inside the map.qml page, but mapViewAsQObject still returned null and app crushed.
Any hints would be really appreciated! Thanks
I have found the solution to my problem, when the next button is clicked, I have to nav push the page using C++, this way it can find the right mapView pointer.
QmlDocument *qml = QmlDocument::create("asset:///map.qml").parent(this);
qml->setContextProperty("_mapViewTest", this);
Page *root = qml->createRootObject<Page>();
QObject* mapViewAsQObject = root->findChild<QObject*>(QString("mapViewObj"));
... etc
nav->push(root);

QTreeView & QAbstractItemModel & insertRow

I'm trying to implement QAbstractItemModel for QTreeView. I have problem with inserting rows.
I noticed that if I insert at the beginning of my application all works fine. But If I insert rows later - after some other operations (like selections etc.) new items stay invisible. Moreover QTreeView seems to doesn't work at all! Do I have to emit some signals to notify QTreeView about rows insertion?
This is my insertion method:
bool LayersModel::insertRows(int position, int count, const QModelIndex & parent)
{
LayersModelItem * parentItem = getItem(parent);
if (position > parentItem->childCount())
return false;
beginInsertRows(parent,position,position+count-1);
bool result = true;
for (;count;--count)
result &= parentItem->insertChildren(position, new LayersModelItem());
endInsertRows();
return result;
}
LayersModelItem is class with QList with its children and data.
Full code of my project (KDE libs needed) is here:
https://github.com/coder89/PhotoFramesEditor/tree/v0.0.8
To see the problem select one of blue item on main window and then right-click on it and select "Delete item". (this method is in Canvas::removeItems()) and it is completly commented - I'm desperate and I've tried to find reason of this problem... (in fact it wasn't delete anything - it adds new item).
Thanks for any help & advice!
Just a quick guess, the QT Doc for QAbstractItemModel says...
The model emits signals to indicate
changes. For example, dataChanged() is
emitted whenever items of data made
available by the model are changed.
Changes to the headers supplied by the
model cause headerDataChanged() to be
emitted. If the structure of the
underlying data changes, the model can
emit *layoutChanged() to indicate to
any attached views that they should
redisplay any items shown, taking the
new structure into account*.
So i guess, you need to emit layoutChanged() signal from your model (whenever you change the data in model) in order to update connected views.
Also read the QT docs for model view architecture, how it is implemented in QT
see if that helps, if it doesn't i will try to download your code and debug it and see, what's wrong.
Good Luck

Resources