I'd like to subclass QListWidgetItem, but I don't get what I'm doing wrong.
I subclassed QListWidget without any trouble using the same principles.
This is my header file :
#ifndef LSPROLISTITEM_H
#define LSPROLISTITEM_H
#include <QObject>
#include <QListWidgetItem>
class LsproListItem : public QListWidgetItem
{
Q_OBJECT
public:
explicit LsproListItem(QString &text, QObject *parent = 0);
signals:
public slots:
};
#endif // LSPROLISTITEM_H
and this is my cpp file :
#include "lsprolistitem.h"
#include <QListWidgetItem>
LsproListItem::LsproListItem(QString & text, QObject *parent) :
QListWidgetItem(text, parent)
{
}
I don't get the argument from my custom constructor, to create an object based on QListWidgetItem..
I try to create is this way :
LsproListItem *simpleText = new LsproListItem("Lorem ipsum");
But this fails with :
appcms.cpp: error : no matching constructor for initialization of 'LsproListItem'
LsproListItem *simpleText = new LsproListItem("Lorem ipsum");
^ ~~~~~~~~~~~~~
lsprolistitem.h:7: candidate constructor (the implicit copy constructor) not viable: no known conversion from 'const char [12]' to 'const LsproListItem' for 1st argument
class LsproListItem : public QListWidgetItem
^
lsprolistitem.h:: candidate constructor not viable: no known conversion from 'const char [12]' to 'QString &' for 1st argument
explicit LsproListItem(QString &text, QObject *parent = 0);
^
Quick solution(not the best): don't use reference:
public:
explicit LsproListItem(QString text, QObject *parent = 0);
//...
LsproListItem::LsproListItem(QString text, QObject *parent) :
Or
public:
explicit LsproListItem( const QString &text, QObject *parent = 0);
//...
LsproListItem::LsproListItem( const QString &text, QObject *parent) :
But there is another mistake. Remove Q_OBJECT macro because QListWidgetItem is not a QObject subclass and you can't use here signal and slots.
To prove: http://qt-project.org/doc/qt-5/qlistwidgetitem.html
Related
is it possible to initialize a new QThread with a parameter?? I created two threads in MainWindow. Now i want initialize the Thread "mySave" with a QString. But it doesn't work.
void MainWindow::on_startButton_clicked()
{
thread = new QThread();
mThread = new myThread();
threadSave = new QThread();
mSave = new mySave("HelloWorld");
....
}
I change the line mSave = new mySave() to mySave = new mySave("HelloWorld") and the constructor of mySave-Class to
mySave::mySave(QObject *parent, QString stringFromMainWindow)
: QObject{parent}, m_string{stringFromMainWindow}
{
this->stringMySave = stringFromMainWindow;
}
mySave.h
class mySave : public QObject
{
Q_OBJECT
public:
explicit mySave(QObject *parent = nullptr, QString stringFileName = NULL);
private:
QFile file;
QString m_string {};
....
But i become a fault!
mainwindow.cpp:57:17: error: no matching constructor for initialization of 'mySave'
mysave.h:9:7: note: candidate constructor (the implicit copy constructor) not viable: no known conversion from 'const char [6]' to 'const mySave' for 1st argument
mysave.h:13:14: note: candidate constructor not viable: no known conversion from 'const char [6]' to 'QObject *' for 1st argument
What's wrong...??
Without QString parameter everthing works fine:
void MainWindow::on_startButton_clicked()
{
thread = new QThread();
mThread = new myThread();
threadSave = new QThread();
mSave = new mySave();
connect(thread, SIGNAL(finished()), mThread, SLOT(deleteLater()));
connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
connect(mThread, SIGNAL(emitData(const QByteArray &)), this, SLOT(setEditText(const QByteArray &)));
mThread->moveToThread(thread);
thread->start();
mSave->moveToThread(threadSave);
threadSave->start();
QMetaObject::invokeMethod(mThread, "interfaceSerial");
QMetaObject::invokeMethod(mSave, "startFile", Q_ARG(QString, fileName));
qDebug() << "Main open " << QThread::currentThread();
dataTimer.start(0); // Interval 0 means to refresh as fast as possible
}
mySave-Thread constructor
mySave::mySave(QObject *parent)
: QObject{parent}
{
qDebug() << "Thread open " << QThread::currentThread();
}
How can i set a QString parameter in the constructor of a new QThread....??
Thanks & Bye bye
The problem was solved...you have to write
class mySave : public QObject
{
Q_OBJECT
public:
explicit mySave(QString stringFileName = NULL, QObject *parent = nullptr);
private:
QFile file;
The last parameter has to be the QObject!!
Look at your mySave constructor signature...
explicit mySave(QObject *parent = nullptr, QString stringFileName = NULL);
The first parameter passed must be a QObject * (or something implicitly convertible to a QObject *) and the second parameter passed must be a QString. If you want mySave to be constructible using a QString only then you must provide a suitable constructor. The obvious option would be to simply reverse the order of the passed parameters...
explicit mySave(QString stringFileName = QString(), QObject *parent = nullptr);
Having said that, the most commonly used constructor calls for classes of this nature would probably be...
/* Default constructor: no text or parent. */
mySave ms1;
/* Text only. */
mySave ms2("Some text...");
/* Parent object only. */
QObject *parent = ...;
mySave ms3(parent);
/* Pass both text and parent object. */
mySave ms4("Some text...", parent);
All of these cases can be provided by the following two constructors...
mySave::mySave (const QString &stringFileName = QString(), QObject *parent = nullptr)
: QObject(parent)
, m_string(stringFileName)
{
...
}
mySave::mySave (QObject *parent)
: QObject(parent)
{
...
}
Or, if you prefer, make the second a delegating constructor...
mySave::mySave (QObject *parent)
: mySave(QString(), parent)
{}
I have two classes: HoodPlanner and DrawRoad. Both classes inherit from QWidget and the the UI so I can access all UI elements in both classes.
Here's a snipped of both classes:
HoodPlanner:
#ifndef HOODPLANNER_H
#define HOODPLANNER_H
#include <ui_hoodplanner.h>
#include "drawroad.h"
#include <QtWidgets>
class HoodPlanner : public QWidget, private Ui::HoodPlanner
{
Q_OBJECT
private:
// (...)
public:
explicit HoodPlanner(QWidget * = 0);
// (...)
private slots:
// (...)
};
#endif // HOODPLANNER_H
DrawRoad:
#ifndef DRAWROAD_H
#define DRAWROAD_H
#include <ui_hoodplanner.h>
#include <QtWidgets>
class DrawRoad : public QWidget, private Ui::HoodPlanner
{
Q_OBJECT
private:
// (...)
public:
explicit DrawRoad(QWidget *parent = 0);
public slots:
void drawSC4Street();
};
#endif // DRAWROAD_H
In HoodPlanner I have a connect that will be emitted whenever the itemSelection changes in table_ts2. I have the slot stored in DrawRoad, but inserting a reference to the DrawRoad object in third position brings up errors.
HoodPlanner::HoodPlanner(QWidget *parent) : QWidget(parent)
{
setupUi(this);
// (...)
DrawRoad *drawRoad = new DrawRoad;
connect(table_ts2, SIGNAL(itemSelectionChanged()), &drawRoad, SLOT(drawSC4Street()));
}
Snipped from DrawRoad.cpp:
#include "drawroad.h"
DrawRoad::DrawRoad(QWidget *parent) : QWidget(parent)
{
}
void DrawRoad::drawSC4Street()
{
if(!table_ts2->selectionModel()->hasSelection()) return;
// (...)
}
And the following errors occur whenever I run the app:
hoodplanner.cpp:12: Fehler: no matching function for call to 'HoodPlanner::connect(QTableWidget*&, const char [24], DrawRoad**, const char [17])'
mingw492_32\include\QtCore\qobject.h:213: Fehler: no type named 'Object' in 'struct QtPrivate::FunctionPointer<const char*>'
(and lots of notes)
The errors don't help me at all and I've searched in several topics for a solution, but none of them had my problem.
Have you tried
HoodPlanner::HoodPlanner(QWidget *parent) : QWidget(parent)
{
setupUi(this);
// (...)
DrawRoad *drawRoad = new DrawRoad;
connect(table_ts2, SIGNAL(itemSelectionChanged()), drawRoad, SLOT(drawSC4Street()));
}
You declrared drawRoad as a pointer:
DrawRoad *drawRoad = new DrawRoad;
And then you pass its address to connect() with &drawRoad.
In this case you are passing a pointer to a pointer address. Try without the &
I am getting this error :
/MyApp/company.cpp:3: error: no matching function for call to ‘DepList::DepList(Company*, Company*)’
Company::Company(QObject *parent) : QObject(parent),dep_cache(this,this),search_results(this,this)
^
/MyApp/company.h:14: In file included from ../../../../MyApp/company.h:14:0,
/MyApp/company.cpp:1: from ../../../../MyApp/company.cpp:1:
/MyApp/deplist.h:34: candidate: DepList::DepList(QAbstractItemModel*, Company*)
explicit DepList(QAbstractItemModel *parent = 0,Company* p_company=0);
/MyApp/deplist.h:34: note: no known conversion for argument 1 from ‘Company*’ to ‘QAbstractItemModel*’
^
when compiling the constructor code for Company class:
Company::Company(QObject *parent) : QObject(parent),dep_cache(this,this),search_results(this,this)
{
// .... some intialization stuff here, unrelated to the error
}
and it only happens with classes where the parent is QAbstractItemModel. With other classes , parents of QObject I am using initialization lists without errors. The error appeared when I added initialization lists.
this is how search_results is declared inside Company class:
class Company : public QObject {
...
DepList search_results;
...
}
and it's constructor is:
DepList::DepList(QAbstractItemModel *parent,Company* p_company) : QAbstractItemModel(parent)
{
company=p_company;
}
What is the problem here , and why it only happens with QAbstractItemModel?? QAbstractItemModel is also a child of QObject and I have no problems with initialization lists in QObject classes. I had a thought of casting , but would it be safe ?
Company is a QObject-derived:
class Company : public QObject {
Q_OBJECT
...
explicit Company(QObject *parent = 0);
...
};
But DepList is a QAbstractItemModel-derived:
class DepList : public QAbstractItemModel
{
...
explicit DepList(QAbstractItemModel *parent = 0,Company* p_company=0);
...
}
What am I missing here, why is the following example giving me compile-time errors?
testline.h:
#include <QLineEdit>
class TestLine : public QLineEdit
{
Q_OBJECT
public:
TestLine(QWidget *parent = 0);
public slots:
virtual void on_textEdited(const QString&);
};
testline.cpp:
#include "testline.h"
TestLine::TestLine(QWidget *parent) : QLineEdit(parent)
{
connect(this, SIGNAL(textEdited(const QString &))), this,
SLOT(on_textEdited(const QString &)));
}
void TestLine::on_textEdited(const QString &text)
{
// something
}
error message:
../testline.cpp:7:5: error
: no matching member function for call to 'connect'
connect(this, SIGNAL(textEdited(const QString &))), this,
^~~~~~~
../../../Qt/5.7/clang_64/lib/QtCore.framework/Headers/qobject.h:219:43: note: candidate function template not viable: requires at least 4 arguments, but 2 were provided
connect(this, SIGNAL(textEdited(const QString &)))
// 1 2 3 321
At this point you are doing what exactly the compiler output says - you are calling connect() with only 2 parameters.
it should be really easy, but something just doesn't work and i can't find the problem.
I have two classes, like below:
1) individualtab
#ifndef INDIVIDUALTAB_H
#define INDIVIDUALTAB_H
#include <QMainWindow>
#include <QInputDialog>
#include <QTableWidget>
#include <QVBoxLayout>
#include <QLabel>
class IndividualTab : public QWidget
{
Q_OBJECT
public:
IndividualTab(QWidget *parent = 0);
QTableWidget *table;
QVBoxLayout *layout;
};
#endif // INDIVIDUALTAB_H
IndividualTab::IndividualTab(QWidget *parent) : QWidget()
{
table = new QTableWidget(0,4);
layout = new QVBoxLayout();
}
2) secondclass
#ifndef SECONDCLASS_H
#define SECONDCLASS_H
#include "individualtab.h"
class secondClass : public QMainWindow
{
Q_OBJECT
public:
explicit secondClass(QWidget *parent = 0);
Ui::secondClass *ui;
~secondClass();
QList<IndividualTab> *individualTabList;
};
#endif // SECONDCLASS_H
secondClass::secondClass(QWidget *parent) : QMainWindow(parent), ui(new Ui::secondClass)
{
ui->setupUi(this);
}
secondClass::~secondClass()
{
delete ui;
}
void secondClass::addNewItem()
{
//Getting parameters
QList<QString> parameters;
//creating QList
//Updating individualTab
for(int i = 0; i < ui->tabWidget->count(); i++)
{
if(parameters.at(0) == ui->tabWidget->tabText(i))
{
IndividualTab tab = individualTabList->at(i);
tab.addItem(parameters);
break;
}
}
}
When i compile i have this error:
In file included from ../secondclass.h:5:0,
from ../secondclass.cpp:1:
/usr/include/qt4/QtGui/qwidget.h: In copy constructor 'IndividualTab::IndividualTab(const IndividualTab&)':
/usr/include/qt4/QtGui/qwidget.h:806:5: error: 'QWidget::QWidget(const QWidget&)' is private
../individualtab.h:10:7: error: within this context
../secondclass.cpp: In member function 'void SecondClass::addNewItem()':
../secondclass.cpp:142:56: note: synthesized method 'IndividualTab::IndividualTab(const IndividualTab&)' first required here
I tought that the problem was in IndividualTab tab = individualTabList->at(i);
So i changed in
IndividualTab *tab = new IndividualTab();
tab = individualTabList->at(i);
but in this case, i had this error:
../secondclass.cpp: In member function 'void SecondClass::addNewItem()':
../secondclass.cpp:143:42: error: cannot convert 'const IndividualTab' to 'IndividualTab*' in assignment
../secondclass.cpp:144:17: error: request for member 'addItem' in 'tab', which is of non-class type 'IndividualTab*'
In file included from ../secondclass.h:5:0,
from ../secondclass.cpp:1:
/usr/include/qt4/QtGui/qwidget.h: In copy constructor 'IndividualTab::IndividualTab(const IndividualTab&)':
../individualtab.h:10:7: instantiated from 'void QList<T>::node_construct(QList<T>::Node*, const T&) [with T = IndividualTab]'
/usr/include/qt4/QtCore/qlist.h:512:13: instantiated from 'void QList<T>::append(const T&) [with T = IndividualTab]'
../Ripetizioni/secondclass.cpp:112:38: instantiated from here
/usr/include/qt4/QtGui/qwidget.h:806:5: error: 'QWidget::QWidget(const QWidget&)' is private
../individualtab.h:10:7: error: within this context
In file included from /usr/include/qt4/QtCore/qobject.h:50:0,
from /usr/include/qt4/QtGui/qwidget.h:46,
from /usr/include/qt4/QtGui/qmainwindow.h:45,
from /usr/include/qt4/QtGui/QMainWindow:1,
from ../secondclass.h:4,
from ../secondclass.cpp:1:
/usr/include/qt4/QtCore/qlist.h: In member function 'void QList<T>::node_construct(QList<T>::Node*, const T&) [with T = IndividualTab]':
/usr/include/qt4/QtCore/qlist.h:372:58: note: synthesized method 'IndividualTab::IndividualTab(const IndividualTab&)' first required here
thanks in advance!
QWidgets aren't meant to be copied. Your individualTabList should be a container of pointers-to-IndividualTab. And there's no good reason for that member itself to be a pointer. Change the declaration to:
QList<IndividualTab*> individualTabList;
Then you can:
IndividualTab *tab = individualTabList.at(i);