Passing QSqlQueryModel through control class - c++

How would I go about passing QSqlQueryModel from a class that connects and queries the database through the control class or QMainWindow in my attempt and back to the widget needing the information?
I thought I could pass the reference location to the QSqlQueryModel object, but this is not working or I am doing something wrong.
I haven't found any examples showing what I am doing on the Qt Developer page.

Looks like these are just compiler errors, nothing specifically to do with Qt.
In short you are getting your pointers and references mixed up.
Error #1:
cardList = new List(sqlModel->getListModel());
You are passing a reference when the List takes a pointer. Fix your return type from getListModel or fix the above line.
Next, you are not specifying the second argument, i.e. the parent QWidget. Either specify your MainWindow as the parent, pass 0, or fix your constructor's signature to provide a default (generally 0).
Error #2:
List::List(QSqlQueryModel *model, QWidget *parent) : ListUI(parent){
setListItems(&model);
}
You receive the model as a pointer and then attempted to take the address of the pointer. I.e. You're making a double pointer. Change the line to
setListItems(model);
Hope that helps.

Related

Passing object to a function in QT causes Segmentation Fault

Thanks in advance for your time. I'm still new to the coding world, so excuse me if I ask something silly or obvious.
I'm coding a small program with QT for manipulating data of a data base. To represent the data I'm using QTableViews. I have a few of them with the same configuration, so I made a function to configure them:
QT 5.12
void MainWindow::configureTableView(QSqlTableModel *model, QTableView *table, QString DBTable)
{
//Pacients table.
model = new QSqlTableModel(this);
model->setTable(DBTable);
model->setEditStrategy(QSqlTableModel::OnManualSubmit);
model->select();
table->setModel(model);
table->setSortingEnabled(true);
table->setCornerButtonEnabled(true);
table->hideColumn(0);
}
I have some QSqlTableModel defined in the mainwindow.h as:
QSqlTableModel *PatientsTable;
...
I call the function with:
configureTableView(PatientsTable, ui->ClientsTabTableView, "Pacientes");
Program starts and shows OK, but as soon as I try to do anything with the view like set a filter
PatientsTable->setFilter(Search)
where Search is a QString configured by other function based on user input, the program crashes and QT tells me it received a signal from the operative system: SIGSEGV (segmentation fault).
Now when is was coding all of this, at some point I had:
void MainWindow::configureTableView(QTableView *table, QString DBTable)
PatientsTable = new QSqlTableModel(this);
PatientsTable->setTable(DBTable);
PatientsTable->setEditStrategy(QSqlTableModel::OnManualSubmit);
PatientsTable->select();
table->setModel(PatientsTable);
table->setSortingEnabled(true);
table->setCornerButtonEnabled(true);
table->hideColumn(0);
which works with no issues at all.
What am I missing? I've been digging for a while now and the code and explanations I found on Internet aren't working.
Thanks again for your time!
The problem is that your function does not modify the PatientsTable variable. Just passing a pointer to a function does not let you modify the pointer itself (only what it's pointing to). Simple solution, pass the pointer by reference.
void MainWindow::configureTableView(QSqlTableModel *&model, QTableView *table, QString DBTable)
The alternative (better in my view) would be to return the pointer from the function
QSqlTableModel *MainWindow::configureTableView(QTableView *table, QString DBTable)
{
QSqlTableModel *model = new QSqlTableModel(this);
...
return model;
}
PatientsTable = configureTableView(ui->ClientsTabTableView, "Pacientes");
It's a very common beginner misunderstanding. Pass pointers to modify what is being pointed to. The pointer itself cannot be modified. In this regard pointers are just like any other kind of variable.

Referencing QWidget from different class

I'm sorry if this question is duplicate but i am really struggling with finding any answer.
Please take in mind that i am novice in c++ programmming.
My problem is this. I have an GUI made in QtCreator. There are two listeners binding keyReleaseEvent, one on main class (SuperFalcon) , one on QTextEdit ( which is separate and modified class ). I have QFrame which i would like to toggle hide/show on "Ctrl + f" key event. Since that QFrame (object name is findWidget) widget belongs to SuperFalcon->ui, there's no problem, everything works fine, problem starts when i try to make "Ctrl + f" in QTextEdit because it's separate event listener. Basically i tried this.
main class name is "SuperFalcon" so:
in superfalcon.h i've made an public static pointer like this:
public:
static QFrame *fWidget;
then in superfalcon.cpp, i firstly execute
ui->findWidget->hide(); and then
fWidget = ui->findWidget; hoping to get pointer on widget.
Next in my QTextEdit class in keyReleaseEvent function i've tried to get that pointer like SuperFalcon::fWidget->show() but i get undefined reference on it.
So , to make things simpler, i don't know how , if possible, to get reference of QFrame widget which is part of one class (SuperFalcon), from another class (QTextEdit class) in order to execute some commands on QFrame.
If it's not clear enough i can provide some code.
You must have a definition of any static member variable.
This definition has to be in a source file because of the one definition rule.
Simply add the line:
QFrame* SuperFalcon::fWidget;
to "superfalcon.cpp".
You have to initialize your static variable, in superfalcon.cpp:
QFrame* SuperFalcon::fWigdet = nullptr;

Qt connect doesn't recognize with lambda expression

I'm designed a QTableWidget with QPushButton, I would like to connect these buttons with a slot to hide some rows.
I'm using a lambda expression to pass a number of a row. But the compiler doesn't recognized this expression :
connect(this->ui->tableWidget->cellWidget(i,0),&QPushButton::clicked,[this,i]{hideRows(i);});
I have this error:
error: no matching function for call to 'SoftwareUdpater::MainWidget::connect(QWidget*, void (QAbstractButton::*)(bool), SoftwareUdpater::MainWidget::displayTable()::<lambda(int)>)'
The function hideRows(int) is declared as a function. And, as a slot, it doesn't work,
CONFIG += c++11 is added in pro file,
My class MainWidget inherits from QWidget,
Q_OBJECT is added in the header.
So I don't udnerstand why connect() is not recognized by Qt 5.9.1 MinGw 32bit.
Edit: [this,i]() instead of [this](const int i) for the lambda expression
Your connection is wrong. You can't connect a function that doesn't take parameters (clicked()) with a function that takes parameters (your lambda). To verify that this is the case, just do this:
connect(this->ui->tableWidget->cellWidget(i,0),&QPushButton::clicked,[this](){});
And see that it will compile. You have to make your design in such a way that signals and slots are compatible.
Also avoid using lambdas in signals and slots. Read the caveats here.
I was reading your comments on the accepted answer and noticed the root of the problem: This error is being thrown because the effective type of the object — as supplied to QObject::connect; i.e QWidget in your case — does not define the referenced signal QPushButton::clicked.
What likely happened is that the QPushButton pointer was cast into a QWidget and then that pointer was given to connect instead of the original which defines the signal.
Cast the pointer back to a QPushButton * and the error should go away.

Using QtScript outside my main form

I am using Qt5, and trying to learn on how to make an application scriptable.
For this I created a main window that contains some text edits, labels, etc. I then added an option called "script console" to that forms' menu in order for me to open a second form containing just a text edit and a button called "Evaluate".
What I was aiming at was being able to use that second form and through Qt script engine be able to set or get values from my main form, and generally be able to script various functions.
What I tried doing was set up the engine like this
scriptingconsole::scriptingconsole(QWidget *parent) :
QDialog(parent),
ui(new Ui::scriptingconsole)
{
ui->setupUi(this);
QScriptValue appContext = myScriptEngine.newQObject(parent);
myScriptEngine.globalObject().setProperty("app", appContext);
}
I don't get what I was expecting though.
If I try to evaluate the expression "app" I get null as an output.
This works fine if I use myScriptEngine.newQObject(parent) with an object inside the current class (if instead of parent I enter this), but I want to be able to access object in other classes too (hopefully all public slots that are used by my app in general).
Does anyone know what I am doing wrong here and how can I use my scripting console
class to access public slots from my main window?
What's wrong?
I guess it's because you didn't explicitly pass the pointer, which points to your main form, to the constructor of your scriptingconsole. That's why you got NULL as a result. (NULL is default, as you can see QWidget *parent = 0 in every QWidget constructor)
This happens if your object is not dynamically instantiated.
Solution
Dynamically allocate scriptingconsole in your main form:
scriptingconsole* myScriptConsole;
//...
myScriptConsole = new scriptingconsole(this);
// ^^^^
// pass the pointer which points to parent widget
The Qt documentation of QScriptEngine::newQObject says:
Creates a QtScript object that wraps the given QObject object, using the given ownership. The given options control various aspects of the interaction with the resulting script object.
http://qt-project.org/doc/qt-4.8/qscriptengine.html#newQObject
i.e. it wraps a QObject.. You are probably passing NULL to newQObject, for whatever reason. Try setting a breakpoint and evaluating the value of 'parent'.

QGraphicsScene, error by accessing pointer in custom class

I have a pretty complex problem... In Qt I have a custom class (named FotoGebouw) that inherits from QGraphicsItem, it also contains a pointer to another custom class (named Gebouw). If I want to acces the selected items from the scene, in other words the "FotoGebouw"items, I first have to cast them to QGraphicsItems. But this way, I seem to lose the pointer (called linkGebouw) that they were pointing to.
Does anyone know a way to get the FotoGebouw items that are selected from the scene, while I can still get the
QList<QGraphicsItem *>bordSceneGebouwen=bordscene->selectedItems();
FotoGebouw *teVerplaatsenFoto=dynamic_cast<FotoGebouw *>(bordSceneGebouwen[0]);
Gebouw *teVerplaatsen=teVerplaatsenFoto->linkGebouw;