I have the following code:
filter.h
#pragma once
#include <QObject>
#include <QSortFilterProxyModel>
class FilterModel : public QSortFilterProxyModel
{
Q_OBJECT
public:
explicit FilterModel(QObject *parent = 0);
Q_INVOKABLE QString getText (QString text);
};
filter.cpp
#include "filter.h"
#include <QDebug>
FilterModel::FilterModel(QObject *parent) : QSortFilterProxyModel(parent) {}
QString FilterModel::getText(QString text)
{
QString qmltext = text;
qmltext != NULL ? qDebug() << qmltext
: qDebug() << "TEXT = NULL";
return qmltext;
}
main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include "abonentstable.h"
#include "filter.h"
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
AbonentsSqlModel *abonentsSqlModel = new AbonentsSqlModel;
abonentsSqlModel->setQuery("SELECT * FROM abonents");
FilterModel *filterModel = new FilterModel;
filterModel->setSourceModel(abonentsSqlModel);
filterModel->setFilterKeyColumn(0);
filterModel->setFilterWildcard("9");
QQmlContext *context = engine.rootContext();
context->setContextProperty("abonents", filterModel);
context->setContextProperty("filter", filterModel);
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
return app.exec();
}
And the .qml file part:
TextField {
id: textField
...
onTextChanged: {
filter.getText(textField.text)
}
...
}
Method getText() gets text (suddenly!) from QML TextField and prints it into debugger, it works fine. But as you can see, I have code for table sorting.
The following problem is: now sorting mask is "9", it works, but I need to return QString qmltext from getText() in some way and put it into filterModel->setFilterWildcard() in main.cpp like that:
QString qmlText = filterModel.getText(QString);
...
filterModel->setFilterWildcard(qmlText);
Of course, it's just an example, it doesn't works and I don't know how to do this.
I do not fully understand what you want to do, but I think you need something like that (if you really need the return value):
QString FilterModel::getText(const QString& text)
{
setFilterWildcard(text);
return text;
}
By the way:
qmltext != NULL
is not working. Use instead:
qmltext.isEmpty() == false
Related
I am new in QT 4 C++ .I have QT 4 form with QTabwidget like in th picture below.
enter image description here
I want to disply on console the string "aa" by selecting tab on clicking it.
Here is my newForm.h
#ifndef _NEWFORM_H
#define _NEWFORM_H
#include "qwidget.h"
#include "ui_newForm.h"
class newForm : public QDialog {
Q_OBJECT
public:
newForm();
virtual ~newForm();
private:
Ui::newForm widget;
};
#endif /* _NEWFORM_H */
_________________________________________________________________________________________
Here is my main.cpp
#include <QApplication>
#include "newForm.h"
int main(int argc, char *argv[]) {
// initialize resources, if needed
// Q_INIT_RESOURCE(resfile);
QApplication app(argc, argv);
newForm *a = new newForm();
a->show();
// create and show your widgets here
return app.exec();
}
Here is my newForm.cpp
#include "newForm.h"
#include <iostream>
#include <QDebug>
newForm::newForm() {
connect(widget.tabWidget, SIGNAL(stateChanged()), this, SLOT(onTabChanged(int)));
widget.setupUi(this);
}
newForm::~newForm() {
}
void newForm::onTabChanged(int ){
qDebug()<<"aa"<<"\n";
}
On selecting new tab it is not displaying "aa"?How to qDebug() "aa" on console?
First of all, why are you using Qt 4 in 2022?
Next thing use currentIndexChanged(int) istead of stateChanged().
Did you also noticed that stateChanged() doesnt pass an interger which is needed for onTabChanged(int).
You also connected widget.tabWidget which isn't initilized yet.
This code might work:
newForm.h
#ifndef _NEWFORM_H
#define _NEWFORM_H
#include "qwidget.h"
namespace Ui { class newForm; };
class newForm : public QDialog {
Q_OBJECT
public:
newForm();
~newForm();
private:
Ui::newForm *widget;
};
#endif /* _NEWFORM_H */
newForm.cpp
#include "newForm.h"
#include "ui_newForm.h"
#include <QDebug>
newForm::newForm()
: widget(new Ui::newForm)
{
widget->setupUi(this);
connect(widget->tabWidget, SIGNAL(currentChanged(int)), this, SLOT(onTabChanged(int)));
}
void newForm::onTabChanged(int ){
qDebug() << "aa";
}
newForm::~newForm() {
delete widget;
}
main.cpp
#include <QApplication>
#include "newForm.h"
int main(int argc, char *argv[]) {
// initialize resources, if needed
// Q_INIT_RESOURCE(resfile);
QApplication app(argc, argv);
newForm a;
a.show();
// create and show your widgets here
return app.exec();
}
I managed to write the data to the objects from the file. But I can't output this data to the console.
I already wrote-ListtForLoad.first().getName()
But can't get such access
I attach the full code of my program.
#include <QCoreApplication>
#include <QFile>
#include <QString>
#include <QDebug>
#include <QIODevice>
#include <QLinkedList>
#include <QTextStream>
class items : public QObject {
public:
items(QString name, QString gryp){
this->name = name;
this->gryp = gryp;
};
QString getName() {
return name;
}
QString getGryp() {
return gryp;
}
private:
QString name;
QString gryp;
};
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QLinkedList<QObject*> ListtForLoad;
QFile fileIn("myfile.txt");
QFile fileOut("myfileout.txt");
int lineCount = 0;
if (fileIn.open(QIODevice::ReadOnly | QIODevice::Text)){
lineCount = QTextStream(&fileIn).readAll().split('\n').count();
fileIn.close();
}
QString stringAllRead;
if(fileIn.open(QIODevice::ReadOnly)) {
stringAllRead = fileIn.readAll();
QStringList splitLines = stringAllRead.split("\r\n");
for (int countItems = 0; countItems < lineCount; countItems++) {
QStringList people = splitLines[countItems].split("&");
ListtForLoad << new items( people[0], people[1] );
}
qDebug() << ListtForLoad.first().getName();
fileOut.close();
}
return a.exec();
}
You are storing QObject*'s in the QLinkedList so when you do the below, you get a QObject* back:
ListtForLoad.first() // <- QObject* here
.getName()
To derefernce a pointer, you need to use -> instead of . so:
ListtForLoad.first()->getName();
However, QObject doesn't have a getName() member function.
You should probably store your own type (items) in the list instead:
QLinkedList<items*> ListtForLoad;
Using QTCreator, I created the design of a GUI Application. I want to read the input entered by the user from lineEdit and when pushButton is clicked, it should print the factorial of that entered number on the same page. I've read some tutorials but don't understand how to code this using qtc++.
A minimal example is like that:
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QWidget>
#include <QLineEdit>
#include <QPushButton>
#include <QHBoxLayout>
class MainWindow : public QWidget
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void hClicked();
void hTextEdit(const QString& data);
private:
QString m_linedata;
QPushButton button;
QLineEdit lineEdit;
QHBoxLayout layout;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include <iostream>
MainWindow::MainWindow(QWidget *parent)
: QWidget(parent)
{
layout.addWidget(&lineEdit);
layout.addWidget(&button);
this->setLayout(&layout);
connect(&lineEdit, &QLineEdit::textChanged, this, &MainWindow::hTextEdit);
connect(&button, &QPushButton::clicked, this , &MainWindow::hClicked);
}
MainWindow::~MainWindow()
{
}
static unsigned factorial(unsigned n)
{
unsigned result = 1;
for (unsigned i=1; i <= n; i++) {
result *= i;
}
return result;
}
void MainWindow::hClicked()
{
if (m_linedata.size() > 0) {
bool res ;
int toint = m_linedata.toInt(&res);
if (res) {
unsigned fact_result = factorial(toint);
lineEdit.clear();
lineEdit.setText(QString::number(fact_result)); }
}
}
void MainWindow::hTextEdit(const QString &data)
{
m_linedata = data;
}
and main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
Just do anything you like with the data passed to the auxillary buffer.
I'm trying to get info from a new type -Cloud- instantiated in the example.qml from my main.cpp.
I have no error of compilation neither of execution. I have only my empty object cloud.
Here my cloud.h
#ifndef CLOUD_H
#define CLOUD_H
#include <QtQuick/QQuickPaintedItem>
#include <QColor>
class Cloud: public QObject
{
Q_OBJECT
Q_PROPERTY(QString name READ name WRITE setName)
Q_PROPERTY(QColor color READ color WRITE setColor)
public:
Cloud(QObject *parent=0);
QString name() const;
void setName(const QString &name);
QColor color() const;
void setColor(const QColor &color);
private:
QString m_name;
QColor m_color;
};
#endif
Here my cloud.cpp
#include "cloud.h"
#include <QPainter>
Cloud::Cloud(QObject *parent)
:QObject(parent)
{
}
QString Cloud::name() const{
return m_name;
}
void Cloud::setName(const QString &name)
{
m_name = name;
}
QColor Cloud::color() const
{
return m_color;
}
void Cloud::setColor(const QColor &color)
{
m_color = color;
}
Here my main.cpp
#include "cloud.h"
#include <QtQuick/QQuickView>
#include <QApplication>
#include <QQmlApplicationEngine>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
qmlRegisterType<Cloud>("Sky", 1,0,"Cloud");
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/example.qml")));
QQmlComponent component(&engine, QUrl((QStringLiteral("qrc:/example.qml"))));
Cloud *cloud = qobject_cast<Cloud*>(component.create());
if(cloud){
qWarning() << "The cloud is "<< cloud->name();
}else{
qWarning() << "there is no cloud" <<cloud;
}
return app.exec();
}
And finally, here my example.qml
import QtQuick 2.0
import Sky 1.0
Item {
width: 300
height: 200
Item{
Cloud{
id:aCloud
name: "Cumulus"
}
}
}
I tried to solve my problem following those tutorials :
Defining QML types
Extending QML example
Thank you for your help :)
When you do component.create(); you are creating an Item which has Cloud as a child. If you want to get Cloud you should do something like:
QObject* myObject = component.create();
QQuickItem* item = qobject_cast<QQuickItem*>(myObject);
Cloud *cloud = item->findchild<Cloud*>();
EDITED: Updated with coyotte508 remarks.
Thanks to Coyotte508 and perencia I succedded to find what was wrong :
in my main.cpp I had a QApplication instead of a QGuiApplication
I have some problems with qt + qml: Sometimes I get segmentation fault trying to clear QQmlengine. And even if program doesn't crashes it has memory leaks. (sorry for my english). I think so because the larger qml file (many nested classes) the larger application process even after QQmlengine was deleted. So my question is what am I doing wrong in the code below?
//main.cpp
#include "qmlwindow.h"
#include <QApplication>
int QMLwindow::someValue = 0;
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
a.setQuitOnLastWindowClosed(false);
QSharedPointer<QMLwindow> w0(new QMLwindow);
QSharedPointer<QMLwindow> w1(new QMLwindow);
QSharedPointer<QMLwindow> w2(new QMLwindow);
w0.data()->createWindow();
w0.data()->createWindow();
w1.data()->createWindow();
w2.data()->createWindow();
return a.exec();
}
//qmlwindow.h
#ifndef QMLWINDOW_H
#define QMLWINDOW_H
#include <QObject>
#include <QQmlComponent>
#include <QQuickWindow>
#include <QSharedPointer>
#include <QDebug>
#include <QQmlEngine>
#include <QQmlContext>
class QMLwindow : public QObject
{
Q_OBJECT
private:
QSharedPointer<QQmlEngine> engine;
QQmlComponent *component;
QObject *windowObj;
QQuickWindow *loginWindow;
QVariant count;
public:
static int someValue;
explicit QMLwindow(QObject *parent = 0);
~QMLwindow();
void createWindow();
signals:
public slots:
void deleteWindow(QQuickCloseEvent *event);
};
#endif // QMLWINDOW_H
//qmlwindow.cpp
#include "qmlwindow.h"
QMLwindow::QMLwindow(QObject *parent) : QObject(parent)
{
someValue++;
count = someValue;
qDebug() << "construction of QMLwindow " << count.toInt();
}
QMLwindow::~QMLwindow()
{
qDebug() << "destruction of QMLwindow " << count.toInt();
}
void QMLwindow::createWindow(){
if(engine.isNull()){
engine = QSharedPointer<QQmlEngine>(new QQmlEngine);
component = new QQmlComponent(engine.data());
component->loadUrl(QUrl(QStringLiteral("qrc:/SomeWindow.qml")));
Q_ASSERT(component->isReady());
windowObj = component->create();
loginWindow = qobject_cast<QQuickWindow *> (windowObj);
engine.data()->rootContext()->setContextProperty("someValue", count) ;
connect(loginWindow, SIGNAL(closing(QQuickCloseEvent*)), SLOT(deleteWindow(QQuickCloseEvent*)));
loginWindow->show();
}
else loginWindow->show();
}
void QMLwindow::deleteWindow(QQuickCloseEvent*event){
Q_UNUSED(event)
if(!engine.isNull()) {
qDebug() << "loginWindow destruction";
engine.clear();
}
this->deleteLater();
}
//SomeWindow.qml
import QtQuick 2.3
import QtQuick.Window 2.0
Window {
width: 200
height: 200
color: "white"
Text{
text: someValue
anchors.centerIn: parent
}
}