I'm looking for an example on how to process the QObject pointer received by the slot when a 'focusObjectChanged' signal is received. I'd like to know how to identify the object that is getting the focus?
The documentation indicates that the pointer is to the focus'd object, but how do I know which?
I've tried analysing the pointer in the debugger but I don't see anything obvious.
You can determine the class and object name, widget properties...:
QObject::connect(qApp, &QApplication::focusObjectChanged, [](QObject *obj){
if(obj) qWarning() << obj->metaObject()->className() << obj->objectName();
QWidget* widget = qobject_cast<QWidget*>(obj);
if(widget) qWarning() << widget->geometry(); // or other properties
BaseType* baseType = qobject_cast<BaseType*>(obj);
if(baseType) qWarning() << baseType->some_actions();
Type1* type1 = qobject_cast<Type1*>(obj);
if(Type1) qWarning() << type1->some_actions();
Type2* type2 = qobject_cast<Type2*>(obj);
if(Type2) qWarning() << type2->some_actions();
});
or compare the pointer with widget pointers to find the widget you need:
SomeClassSlot(QObject* obj) {
if(!obj) return;
if(obj == m_mainWindows) ....
if(obj->parent() == m_table) ...
MyClass myClass = qobject_cast<MyClass*>(obj);
if(myClass) myClass->some_method();
}
Related
(Sorry for my future mistakes, I am fr)
I am creating a program to control the pc with a phone in c++ ( https://github.com/titicplusplus/PCPhone )
The server part works good, so I would like to add graphics interfaces for user, I decided to use QT.
When I stop the program QT, I have segmentation fault, and that stops the server.
My function that run the graphic interface:
void start_g(int argc, char **argv)
{
QApplication app(argc,argv);
QFile File(":/stylesheet.qss");
File.open(QFile::ReadOnly);
QString StyleSheet = QLatin1String(File.readAll());
app.setStyleSheet(StyleSheet);
ui interface; //QWidget window
interface.start();
std::cout << "start gui" << std::endl;
interface.show();
app.exec(); //return 0
return;
}
I encapsulated the code in a function because I would like to run it in a thread, but for now during my tests, I don't do this.
Here is my class ui (user interface)
class ui : public QWidget
{
public:
ui();
void start();
void change_file();
void change_port();
void freset();
void fapply();
~ui(); //Here is the problem
private:
openf f_open; //to open the extension s file
std::unique_ptr<QFormLayout> formLayout;
std::unique_ptr<QGroupBox> settings;
std::unique_ptr<QVBoxLayout> main_layout;
std::unique_ptr<QGridLayout> grid;
std::vector< std::vector< std::unique_ptr<QPushButton> > > tab2d;
std::unique_ptr<QGroupBox> all_button;
std::unique_ptr<QSpinBox> port_l;
std::unique_ptr<QPushButton> button_f;
std::unique_ptr<QPushButton> apply;
std::unique_ptr<QPushButton> reset;
std::unique_ptr<QLabel> text_f;
};
and the important function
ui::ui() {}
void ui::start()
{
grid = std::make_unique<QGridLayout>( this );
/** Code that you don't need, I guess **/
std::cout << "setting part" << std::endl;
all_button = std::make_unique<QGroupBox>("Slide");
all_button->setLayout(grid.get());
port_l =std::make_unique<QSpinBox>(this);
port_l->setMaximum(65535);
port_l->setValue( config_json["port"].get<int>() );
button_f = std::make_unique<QPushButton>("Open Image" ,this);
text_f = std::make_unique<QLabel>(QString::fromStdString(fs::absolute(config_json["image"].get<std::string>())), this);
apply = std::make_unique<QPushButton>(" Apply " ,this);
reset = std::make_unique<QPushButton>("Reset" ,this);
//connect(button_f.get(), &QPushButton::clicked, this, &ui::change_file);
connect(port_l.get(), QOverload<int>::of(&QSpinBox::valueChanged), this, &ui::change_port);
connect(apply.get(), &QPushButton::clicked, this, &ui::fapply);
connect(reset.get(), &QPushButton::clicked, this, &ui::freset);
//auto fileName = QFileDialog::getOpenFileName(&window, "Open Image", "/home/$USER", ("Image Files (*.png *.jpg *.bmp)"));
formLayout = std::make_unique<QFormLayout>();
formLayout->addRow("The port", port_l.get());
formLayout->addRow(text_f.get(), button_f.get()); //////////The problem is here
//formLayout->addRow(apply.get(), reset.get());
settings = std::make_unique<QGroupBox>("Settings");
settings->setLayout(formLayout.get());
main_layout = std::make_unique<QVBoxLayout>(this);
main_layout->addWidget(all_button.get());
main_layout->addWidget(settings.get());
}
So I saw on google, the problem come with the pointers and they destroy, so I try this
ui::~ui()
{
main_layout = nullptr;
settings = nullptr;
all_button = nullptr;
std::cout << "1u" << std::endl;
button_f = nullptr;
std::cout << "2u" << std::endl;
text_f = nullptr;
std::cout << "3u" << std::endl;
apply = nullptr;
std::cout << "4u" << std::endl;
reset = nullptr;
std::cout << "5u" << std::endl;
grid = nullptr;
std::cout << "bye bye" << std::endl;
}
The result is [ the errors are in french so I try to translate it ]:
1u
Segmentation fault (core dumped)
When I delete this line formLayout->addRow(text_f.get(), button_f.get());
There are no problems the deference of text_f and button_f, but with the next (apply and reset).
1u
2u
3u
Segmentation fault (core dumped)
On google, I saw a lot a time the people use the pointer and not the std::unique_ptr with qt, but I saw in general that people say "the smart pointer are better than the normal pointer.
Also, I try to replace button_f.get() to std::move(button_f).get() but that doesn't work.
For information, I am on Ubuntu 20.04, with g++-9.3 and qt5.
Thank you in advance for your answers !
Thanks G.M. and vahancho
I understand why I don't need to do std::unique_ptr .
When I learn C++ and pointer, there are rule : write one delete for every new, or better, use the smart pointer.
But with this two topics:
https://codereview.stackexchange.com/questions/43189/deleting-pointers-in-qt5/43201
Deleting Pointer to widget Qt C++
For the QT object, it's QT that deletes the pointers when the class is delete.
So I just need to need change all of my std::unique_ptr in raw pointer.
I have a C++ class that creates a QML object, it takes five parameters:
//prototype
// Q_INVOKABLE QQuickItem *createQmlObject(QObject *parent, QString path, QString id="", int x=0, int y=0);
QQuickItem * QmlItemCreator::createQmlObject(QObject* parent,QString path,QString id,int x,int y)
{
QQmlEngine engine;
QQmlComponent component(&engine, QUrl::fromLocalFile(path));
QQuickItem * mainObject_;
if(component.isError())
{
qWarning() << "QmlItemCreator::createQmlObject The QMLComponent for "
<< path << " has errors: " << component.errorString();
}
else if (component.isReady())
{
mainObject_ = qobject_cast<QQuickItem*>(component.create());
QQmlEngine::setObjectOwnership(mainObject_, QQmlEngine::JavaScriptOwnership);
mainObject_->setProperty("id",id);
mainObject_->setProperty("x",x);
mainObject_->setProperty("y",y);
mainObject_->setParent(parent);
// qDebug()<<mainObject_<<" "<<mainObject_->parent()<<" "<<mainObject_->property("x");
}
else
{
componentComplete();
}
return mainObject_;
}
I use it in QML as follows:
Item{
id: idRoot
Component.onCompleted:
{
var obj = qmlCreator.createQmlObject(idRoot,"path/test.qml")
console.log(obj.parent)// print null !!
}
}
While in C++ context, I can correctly print the value of the properties I set, as well as the parent of the created object. However, when I print them from QML, theparenthas anulland the properties areundefined`.
I printed the adress of the created object from C++ and from QML and I've got the same address. I don't understand what's causing this behaviour.
Thanks for your help.
I've solved the issue by adding the following line of code to the C++ code:
mainObject_->setParentItem(qobject_cast<QQuickItem*>(parent));
As openNote is a QAction I can set its object name via setObjectName. Then why can't I have access to the data? I have no clue.
QAction *openNote;
QVariant noteID;
openNote = m_mainContextMenu.addAction(menuEntryName);
openNote->setObjectName("noteEntry"); //QAction::setObjectName
int ID = m_noteList[0].data()->getID();
noteID.setValue(ID);
openNote->setData(noteID); //QAction::setData
connect(openNote,SIGNAL(triggered()),this,SLOT(s_showNote()));
my slot:
void Traymenu::s_showNote(){
QObject* obj = sender(); //sender is "openNote" of type QAction
qDebug() << "objectName" << obj->objectName(); //works, because obj = QAction
obj->data(); //no member data found?! Why? Documentation says there is...
My auto completion shows every member, e.g. setData, objectName, but not data. How can I get access to it?
If I write
QAction bla;
bla.data(); //<== auto completion shows "data"
Where is my problem?
This is the solution that I have found out:
void Traymenu::s_showNote(){
QObject* obj = sender();
QAction *noteEntry = qobject_cast<QAction *>( sender() );
int noteID = noteEntry->data().toInt();
qDebug() << "noteID" << noteID;
}
I have a QTreeWidget in which each of its items has a QComboBox in a column. I have connected it to a slot with a QSignalMapper, and I'm successfully retrieving both the item and the index in the combobox when it is triggered. I did it like this:
foreach(Workplace *wp, allWorkplaces){
QTreeWidgetItem *workplaceItem = new QTreeWidgetItem;
workplaceItem->setText(0, wp->workplaceName());
workplaceItem->setText(1, wp->workplaceDescription());
myWorkplaceUi->treeWidget->addTopLevelItem(workplaceItem);
QComboBox *combo = new QComboBox();
combo->addItems(allShiftModels);
combo->setAutoFillBackground(true);
ShiftModel *shiftModel = qobject_cast<ShiftModel *>(wp->usedShiftModel);
myWorkplaceUi->treeWidget->setItemWidget(workplaceItem,2, combo);
if(shiftModel && !shiftModel->shiftModelName().isEmpty()){
qDebug()<<"after the cast: "<< shiftModel->shiftModelName();
combo->setCurrentIndex(combo->findText(shiftModel->shiftModelName(), Qt::MatchExactly));
}else{
combo->setCurrentIndex(combo->findText("None", Qt::MatchExactly));
}
connect(combo, SIGNAL(currentIndexChanged(int)), signalMapper, SLOT(map()));
signalMapper->setMapping(combo, QString("%1").arg(wp->workplaceName()));
}
connect(signalMapper, SIGNAL(mapped(const QString &)),this, SLOT(changed(const QString &)));
My objective is, after retrieving both the Workplace and the ShiftModel, to update them in the instances of my already created Workplaces. So, basically, I try to find the Workplace and the ShiftModel that were selected, because depending on the ShiftModel selected, I will change a pointer to the ShiftModel in the Workplace class:
class Workplace : public QObject
{
Q_OBJECT
public:
(...)
ShiftModel *usedShiftModel;
(...)
}
And the changed slot:
void workplacesdialog::changed(QString position){
QList<Workplace* > allWorkplaces = this->myProject->listMyWorkplaces();
QList<ShiftModel*> allShiftModels = this->myProject->myFactory->listShiftModels();
foreach(Workplace* workplace, allWorkplaces){
foreach(ShiftModel *shiftmodel, allShiftModels){
qDebug() <<"workplace:"<< workplace->workplaceName();
qDebug() <<"shiftmodel:"<< shiftmodel->shiftModelName();
QString wp = position;
QTreeWidgetItem* item=(QTreeWidgetItem*)myWorkplaceUi->treeWidget->findItems(wp,Qt::MatchExactly,0).at(0);
QComboBox *combo = (QComboBox*)myWorkplaceUi->treeWidget->itemWidget(item,2);
if(combo && item){
QString sm = combo->currentText();
qDebug() << "selected shiftmodel "<< sm << " on workplace "<< wp;
if(workplace->workplaceName()==wp && shiftmodel->shiftModelName()==sm){
workplace->usedShiftModel = shiftmodel;
break;
}
else{
workplace->usedShiftModel = 0;
return;
}
}else{
qDebug() << "cast failed!";
return;
}
}
}
}
So, my problem with this is, when I click one of the comboboxes, successfully retrieve both the item and index selected, but then, when I try to traverse them with the two foreach loops in the slot, it does not work as I expected. I hoped that every time I clicked on an index in one of the comboboxes, this would be called, and it is. Although, for some reason, the method I'm using to match what the user selected with what was already instatiated doesn't work.
Also, it looks like it only hits both the 1st workplace on the allWorkplaces list as well as the 1st shiftmodel on the ShiftModels list, and this is my problem.
If anyone knows how to fix this or has any ideas to share, please let me know. Thank you.
The problem is this:
if(workplace->workplaceName()==wp && shiftmodel->shiftModelName()==sm){
workplace->usedShiftModel = shiftmodel;
break;
}
else{
workplace->usedShiftModel = 0;
return;
}
If either the workplace name does not match, or the shift model name does not match, the relation between the work place and its currently linked shift model is removed and your function returns.
I could restructure the two for-loops for you, but there is an easier and less error-prone way:
Note: I marked some code paths with "TODO" which I skipped due to lack of time. You should be able to figure them out yourself though.
// Set up hashes for quick lookup
QHash< QString, Workplace* > workplaceHash;
QList<Workplace* > allWorkplaces = this->myProject->listMyWorkplaces();
foreach( Workplace* workplace, allWorkplaces )
{
workplaceHash.insert( workplace, workplace->workplaceName() );
}
// TODO: Do a similar thing for the shift models here
// Find the selected workplace
if( !workplaceHash.contains( position ) )
{
// TODO: Error handling (An unknown/No workplace was selected)
return;
}
// else: A valid workplace was selected
Workplace* selectedWorkplace = workplaceHash.value( position );
// TODO: Retrieve the name of the shift model (stored in variable sm)
// Find the selected shiftmodel
if( !shiftplaceHash.contains( sm ) )
{
// No shift model was selected
selectedWorkplace->usedShiftModel= 0;
return;
}
// Else: Both work place and shift model were selected
Shiftplace* selectedShiftModel = shiftplaceHash.value( sm );
selectedWorkplace->usedShiftModel = selectedShiftModel;
A few ideas for refactoring:
You could maybe do the creation of the hashes outside of this method and store them in member variables. Just make sure that the hash is always updated when a workplace is added or removed.
You could make spotting errors easier by extracting parts of the code into separate methods, e.g. QString getSelectedShiftModelName() etc.
So, In the end I figured out my loops were really messed up... This is working now:
void workplacesdialog::changed(QString position){
QList<Workplace* > allWorkplaces = this->myProject->listMyWorkplaces();
QList<ShiftModel*> allShiftModels = this->myProject->myFactory->listShiftModels();
qDebug() << allWorkplaces.size() << " workplaces";
qDebug() << allShiftModels.size() << " ShiftModels";
QString wp = position;
QString sm;
QTreeWidgetItem* item=(QTreeWidgetItem*)myWorkplaceUi->treeWidget->findItems(wp,Qt::MatchExactly,0).at(0);
QComboBox *combo = (QComboBox*)myWorkplaceUi->treeWidget->itemWidget(item,2);
if(combo && item){
sm = combo->currentText();
qDebug() << "selected shiftmodel "<< sm << " on workplace "<< wp;
}else{
qDebug() << "cast failed!";
return;
}
foreach(Workplace* workplace, allWorkplaces){
foreach(ShiftModel *shiftmodel, allShiftModels){
qDebug() <<"workplace:"<< workplace->workplaceName();
qDebug() <<"shiftmodel:"<< shiftmodel->shiftModelName();
if(workplace->workplaceName()==wp && shiftmodel->shiftModelName()==sm){
qDebug() << "found match!: "<< wp << " >>>>> " << sm;
workplace->usedShiftModel = shiftmodel;
return;
}else if(workplace->workplaceName()==wp && sm=="None"){
qDebug() << "clear match: "<< wp << " >>>>> " << sm;
workplace->usedShiftModel = 0;
return;
}
}
}
}
Im trying to make the code which reads xml files and deserialize various qt controls from this xml, and im doing this using QDomDocument, and i want to get the QLlist from my deserealization method. And im having a bit of troubles, here is some code of the template class (.h) file:
QList<T*> deserialize(QIODevice *input)
{
QList<T*> objects = QList<T*>();
if(_deserializeObject(input, objects))
return objects;
}
bool _deserializeObjects(QIODevice* input, QList<QObject*>& list);
and my .cpp file with deserialize method, here im reading the control tags from file:
bool Serializer::_deserializeObjects(QIODevice* input, QList<QObject *> &objects)
{
QDomDocument doc;
if (!doc.setContent(input))
return false;
QDomElement root= doc.documentElement();
for(int j = 0; j < root.childNodes().length();j++)
{
QObject* object;
qDebug() << root.tagName();
if(root.tagName().contains("QGroupBox")) // <------- Here i need to determine which control i need to process.
{
????
}
qDebug () << object->metaObject()->className();
qDebug() << object->metaObject()->propertyCount();
for(int i = 0; i < object->metaObject()->propertyCount(); i++)
{
object->metaObject()->cast()
QMetaProperty prop = object->metaObject()->property(i);
QString propName = prop.name();
if(propName == "objectName")
continue;
QDomNodeList nodeList = root.elementsByTagName(propName);
if(nodeList.length() < 1)
continue;
QDomNode node = nodeList.at(0);
QVariant value = object->property(propName.toLatin1().data());
QString v = node.toElement().text();
if(propName == "x")
{
x = v.toInt();
}
else if(propName == "y")
{
y = v.toInt();
}
else if(propName == "width")
{
width = v.toInt();
}
else if(propName == "height")
{
height = v.toInt();
}
if(propName == "geometry")
{
continue;
}
object->setProperty(propName.toLatin1().data(), QVariant(v));
}
object->setProperty("geometry",QVariant(QRect(x,y,width,height)));
objects.push_back(object);
}
return true;
}
In this part
if(root.tagName().contains("QGroupBox")) // <------- Here i need to determine which control i need to process.
{
????
}
qDebug () << object->metaObject()->className();
qDebug() << object->metaObject()->propertyCount();
for(int i = 0; i < object->metaObject()->propertyCount(); i++)
{
...
}
I want to actually somehow get the type of the control by name, so the question is, can i cast QGroupBox to QObject saving the QGroupBox properties so QObject metaObject class name would be QGroupBox, so i can pass all this properties? Because i don't want to make the loops for each control type. Also i when i got the result like so:
QList<QObject *> ds = s.deserialize<Object>((QIODevice*)&f);
Can i then just pass all QObjects in a loop and using QMetaObject class name and using qobject_cast cast each object to QPushButton,QLabel etc.?
QGroupBox is a subclass of QObject; therefore every QGroupBox is also a QObject, so you can treat it as one whenever you like. An explicit cast isn't necessary.
Iterating over all the diffent objects-derived-from-QObject in a loop will do what you want, provided that the methods you call on them are virtual methods (which they presumably will be -- in particular, QObject::metaObject() is a virtual method, so your loop will get the appropriate QMetaObject returned even if it is calling them method through a QObject pointer).
(As an aside, the annoying part of the process will probably be the part where you have read the name of the object's type from the XML and now need to instantiate an object of that type. AFAIK there is no good automatic way to do that in C++, so the best you can do is a factory function containing a giant switch statement with a separate case for every type you might want to instantiate)
Alternatively, use a right tool for a right job. Chances are that what you are really building here is some XML thing for defining widget layouts etc. Qt already has a tool for that, the Qt Designer which uses an XML format for the UI definitions and a C++ code generator for actually producing a C++ code during compile time.