Hey guys I am trying to make the following slot: The user should input a sequence of numbers in a line edit with spaces between them (for example: 5 10 15 10 2) I want these numbers to be stored into a vector of integers. After they are all read, I want the values of the vector to be changed in the following way - starting from the second element up to the end, every element should be equal to the following: vector[i] = vector[i] - vector[i-1]. Again, this doesn't count for the first one. I will do some more computing, but It would appear that I can't get this done quite right yet.
The layout is the following: a button, a lineEdit and a Label.
This is the header file:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QVector>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
QVector<int> vec;
private slots:
void compute();
};
#endif // MAINWINDOW_H
And the implementation:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QString>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->lineEdit
, SIGNAL(editingFinished()), this, SLOT(compute()));
connect(ui->pushButton
, SIGNAL(pressed()), this, SLOT(compute()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow :: compute()
{
// Read user input
QString numbers = ui->lineEdit->text();
QString tmp; // create temp string which will be used to store the temp substrings of each int
numbers+=" ";
for(int i = 0; i < numbers.length()-1; i++)
{
if(numbers[i].isDigit())
{
tmp +=numbers[i];
}
else
{
bool ok = true;
int iTime = tmp.toInt(&ok, 10);
vec.push_back((iTime));
tmp="";
}
}
//compute the real vector that we will search the max subarray from
for(int i = 1; i < vec.size(); i++)
{
vec[i] = vec[i] - vec[i-1];
}
int tt = 0;
for(int z = 0; z != vec.size(); z++)
{
tt+= vec[z];
}
ui->label->setText( QString::number(tt));
}
I won't post the main function since it is the standard one when you create a Qt GUI aplication. So, the problem is, the following line: ui->label->setText( QString::number(tt)); always prints zero to the label. I made this program in code blocks, without the Qt GUI libraries and I got it working. I am intrigued where the problem is. Thanks in advance.
for(int z = 0; z != vec.size(); z++)
Should be:
for(int z = 0; z < vec.size(); z++)
Related
I am new and really unexperienced with coding, but we got a task at the university where I ran into a problem I could not solve by myself. In one class a vector causes a problem I do not understand. After hours of trial and error I am a bit tired of guessing... So, I try it here and hope for some help.
The program is supposed to draw an diagram based on some Input variables the user can change. At the moment I am trying to implement the painting of the graph. The Input and the calculation of the data works.
One aspect of the task is that we have to safe the calculated data in a vector and pass this vector as a pointer to other functions, for example the painter.
Now, here the problem occures. Any interaction with the vector m_punkte I passed to paintEvent causes a crash of the program. I could narrow the problem down to commands like: vector->begin() or vector->end(). In the instant the program tries to execute them, windows stops the program.
At first I thought the vector could be somehow passed wrong and I am trying to call an empty vector or the address is undefined, but even commands like vector->emty() causing the crash. So I believe it is a more general error which is caused by the framework or something like that.
After running the debugging tool, it also told me that the error is caused by the mentioned commands and further more told me this:
Der Prozess wurde wegen eines Signals vom Betriebssystem angehalten.
Name des Signals : SIGSEGV
Bedeutung : Segmentation fault
I guess that's some kind of memory problem, right?
I would be really happy if someone could help me, so I can work again on the actual task and not guessing why this vector is somehow broken
What I can also tell is that I am working with Qt Creator and the program is running as an QtWidget program if that helps.
Dropbox link to a copy of the programm:
https://www.dropbox.com/sh/l5e220lk5y7jx2z/AAAdG7AjHiiKJxOKWT-zrP8ia?dl=0
I Hope now my question is clear and this helps more to find the origin of the error.
datenpunkt.h
#ifndef DATENPUNKT_H
#define DATENPUNKT_H
#include <vector>
struct datenpunkt
{
float temperatur;
float widerstand;
float laenge;
};
typedef std::vector<datenpunkt> PunktVektor;
#endif // DATENPUNKT_H
diagrammwidget.h
#ifndef DIAGRAMMWIDGET_H
#define DIAGRAMMWIDGET_H
#include "datenpunkt.h"
#include <QWidget>
namespace Ui {
class DiagrammWidget;
}
class DiagrammWidget : public QWidget
{
Q_OBJECT
public:
explicit DiagrammWidget( QWidget *parent = 0);
~DiagrammWidget();
void setPunkte (PunktVektor *punkte);
void setUntere_temp(int untere_temp);
void setObere_temp(int obere_temp);
void setAufloesung(int aufloesung);
PunktVektor *punkte() const;
protected:
void paintEvent(QPaintEvent *event);
private:
Ui::DiagrammWidget *ui;
PunktVektor *m_punkte;
int m_untere_temp = 0;
int m_obere_temp = 0;
int m_aufloesung = 0;
};
#endif // DIAGRAMMWIDGET_H
dialog.h
#ifndef DIALOG_H
#define DIALOG_H
#include "datenpunkt.h"
#include <string.h>
#include <QDialog>
#include <QString>
namespace Ui {
class Dialog;
}
class Dialog : public QDialog
{
Q_OBJECT
public:
explicit Dialog(QWidget *parent = 0);
~Dialog();
void werteberechnung(PunktVektor *m_PunktListe);
float untere_temp() const;
void setUntere_temp(float untere_temp);
float obere_temp() const;
void setObere_temp(float obere_temp);
int aufloesung() const;
void setAufloesung(int aufloesung);
QString werkstoff() const;
void setWerkstoff(QString werkstoff);
float materialkonstante() const;
void setMaterialkonstante(float materialkonstante);
float bezugswiderstand() const;
void setBezugswiderstand(float bezugswiderstand);
float wiederstand() const;
void setWiederstand(float wiederstand);
float temperatur() const;
void setTemperatur(float temperatur);
private slots:
void on_speichern_clicked();
void on_verwerfen_clicked();
void on_zuruecksetzen_clicked();
private:
Ui::Dialog *ui;
PunktVektor *m_PunktListe;
QString m_werkstoff = "A 34-2/30 (SIEMENS)";
float m_materialkonstante = 3440; // B/ K
float m_bezugswiderstand = 5000; // Rto/ Ohm
float m_untere_temp = 0 + 272.15;
float m_obere_temp = 300 + 272.15;
int m_aufloesung = 10;
float m_RT= 0; // Rt/ Ohm
float m_To = 20 + 272.15;
float m_T = 0; // T/ C°
float m_UD; //Untere Dekadengrenze
float m_OD; //Obere Dekadengrenze
};
#endif // DIALOG_H
diagrammwidget.cpp
#include "diagrammwidget.h"
#include "ui_diagrammwidget.h"
#include "datenpunkt.h"
#include <vector>
#include <QtCore>
#include <QtGui>
DiagrammWidget::DiagrammWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::DiagrammWidget){
ui->setupUi(this);
m_punkte = 0;
}
DiagrammWidget::~DiagrammWidget(){
delete ui;
}
void DiagrammWidget::setPunkte(PunktVektor *punkte){
m_punkte = punkte;
}
void DiagrammWidget::paintEvent(QPaintEvent *event)
{
QPainter myPainter (this);
/*if((m_punkte == 0|| m_punkte->size() == 0)){
myPainter.drawText(100,100,"No Points found!");
return;
}*/
datenpunkt ersterPunkt=*m_punkte->begin();
datenpunkt startPunkt = ersterPunkt;
PunktVektor::iterator it;
for( it = m_punkte->begin()++; it != m_punkte->end(); it++){
datenpunkt p = *it;
myPainter.drawLine(startPunkt.temperatur,startPunkt.laenge,
p.temperatur,p.laenge);
startPunkt = p;
}
}
PunktVektor *DiagrammWidget::punkte() const{
return m_punkte;
}
void DiagrammWidget::setAufloesung(int aufloesung){
m_aufloesung = aufloesung;
}
void DiagrammWidget::setObere_temp(int obere_temp){
m_obere_temp = obere_temp;
}
void DiagrammWidget::setUntere_temp(int untere_temp){
m_untere_temp = untere_temp;
}
dialog.cpp
#include "dialog.h"
#include "ui_dialog.h"
#include "datenpunkt.h"
#include "diagrammwidget.h"
#include <QtCore>
#include <QtGui>
#include <QString>
#include <math.h>
#include <iostream>
using namespace std;
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
m_PunktListe = new PunktVektor;
//Standarteinstellungen
ui->werkstoff->setText("A 34-2/30 (SIEMENS)");
ui->materialkonstante->setValue(3440);
ui->materialkonstante->setSuffix(" K");
ui->bezugswiderstand->setValue(5000);
ui->bezugswiderstand->setSuffix(" Ω");
ui->unteretemperatur->setValue(0);
ui->unteretemperatur->setSuffix(" °C");
ui->oberetemperatur->setValue(300);
ui->oberetemperatur->setSuffix(" °C");
ui->aufloesung->setValue(10);
ui->aufloesung->setSuffix(" Δ°C");
ui->unteretemperatur->setMaximum(ui->oberetemperatur->value());
ui->oberetemperatur->setMinimum(ui->unteretemperatur->value());
ui->unteretemperatur->setSingleStep(ui->aufloesung->value());
ui->oberetemperatur->setSingleStep(ui->aufloesung->value());
werteberechnung(m_PunktListe);
//Diagramm aktualisieren und Anzeigen
DiagrammWidget dg;
dg.setUntere_temp(m_untere_temp);
dg.setObere_temp(m_obere_temp);
dg.setAufloesung(m_aufloesung);
dg.setPunkte(m_PunktListe);
dg.show();
}
Dialog::~Dialog()
{
delete m_PunktListe;
delete ui;
}
void Dialog::werteberechnung(PunktVektor *m_PunktListe)
{
datenpunkt p;
float temp = m_untere_temp;
//Anzahl der zu bestimmenden Datenpunkte
int anzahl = (m_obere_temp-m_untere_temp)/m_aufloesung;
//Bestimmen der Dekadengrenzen UD und OD
m_RT = m_bezugswiderstand * pow(M_E, (m_materialkonstante*
((1/m_obere_temp)-(1/m_To))));
m_UD = (int(log10(m_RT)));
m_RT = m_bezugswiderstand * pow(M_E, (m_materialkonstante*
((1/m_untere_temp)-(1/m_To))));
m_OD = (int(log10(m_RT))+1);
//Füllen des Datenvectors
for (int i = 0; i <= anzahl; i++){
m_RT = m_bezugswiderstand * pow(M_E, (m_materialkonstante*
((1/temp)-(1/m_To))));
p.widerstand = m_RT;
p.temperatur = temp;
float x = log10(m_RT);
if(m_UD != 0){
p.laenge = ((x-m_UD)*5);
}
else
p.laenge = x*5;
m_PunktListe->push_back(p);
temp = temp + m_aufloesung;
}
}
void Dialog::on_speichern_clicked()
{
setWerkstoff(ui->werkstoff->text());
setMaterialkonstante(ui->materialkonstante->value());
setBezugswiderstand(ui->bezugswiderstand->value());
setUntere_temp(ui->unteretemperatur->value()+ 272.15);
setObere_temp(ui->oberetemperatur->value()+ 272.15);
setAufloesung(ui->aufloesung->value());
ui->unteretemperatur->setMaximum(ui->oberetemperatur->value());
ui->oberetemperatur->setMinimum(ui->unteretemperatur->value());
ui->unteretemperatur->setSingleStep(ui->aufloesung->value());
ui->oberetemperatur->setSingleStep(ui->aufloesung->value());
//Berechnung des Datenvectors
werteberechnung(m_PunktListe);
//Diagramm aktualisieren
DiagrammWidget dg;
dg.setUntere_temp(m_untere_temp);
dg.setObere_temp(m_obere_temp);
dg.setAufloesung(m_aufloesung);
dg.setPunkte(m_PunktListe);
dg.show();
}
float Dialog::temperatur() const{
return m_T;
}
void Dialog::setTemperatur(float temperatur){
m_T = temperatur;
}
float Dialog::wiederstand() const{
return m_RT;
}
void Dialog::setWiederstand(float wiederstand){
m_RT = wiederstand;
}
float Dialog::bezugswiderstand() const{
return m_bezugswiderstand;
}
void Dialog::setBezugswiderstand(float bezugswiderstand){
m_bezugswiderstand = bezugswiderstand;
}
float Dialog::materialkonstante() const{
return m_materialkonstante;
}
void Dialog::setMaterialkonstante(float materialkonstante){
m_materialkonstante = materialkonstante;
}
QString Dialog::werkstoff() const{
return m_werkstoff;
}
void Dialog::setWerkstoff(QString werkstoff){
m_werkstoff = werkstoff;
}
int Dialog::aufloesung() const{
return m_aufloesung;
}
void Dialog::setAufloesung(int aufloesung){
m_aufloesung = aufloesung;
}
float Dialog::obere_temp() const{
return m_obere_temp;
}
void Dialog::setObere_temp(float obere_temp){
m_obere_temp = obere_temp;
}
float Dialog::untere_temp() const
{
return m_untere_temp;
}
void Dialog::setUntere_temp(float untere_temp)
{
m_untere_temp = untere_temp;
}
void Dialog::on_verwerfen_clicked()
{
ui->werkstoff->setText(m_werkstoff);
ui->materialkonstante->setValue(m_materialkonstante);
ui->bezugswiderstand->setValue(m_bezugswiderstand);
ui->unteretemperatur->setValue(m_untere_temp);
ui->oberetemperatur->setValue(m_obere_temp);
ui->aufloesung->setValue(m_aufloesung);
ui->unteretemperatur->setMaximum(ui->oberetemperatur->value());
ui->oberetemperatur->setMinimum(ui->unteretemperatur->value());
ui->unteretemperatur->setSingleStep(ui->aufloesung->value());
ui->oberetemperatur->setSingleStep(ui->aufloesung->value());
}
void Dialog::on_zuruecksetzen_clicked()
{
//Standarteinstellungen
ui->werkstoff->setText("A 34-2/30 (SIEMENS)");
ui->materialkonstante->setValue(3440);
ui->bezugswiderstand->setValue(5000);
ui->unteretemperatur->setValue(0);
ui->aufloesung->setValue(10);
ui->oberetemperatur->setValue(300);
setWerkstoff(ui->werkstoff->text());
setMaterialkonstante(ui->materialkonstante->value());
setBezugswiderstand(ui->bezugswiderstand->value());
setUntere_temp(ui->unteretemperatur->value()+ 272.15);
setObere_temp(ui->oberetemperatur->value()+ 272.15);
setAufloesung(ui->aufloesung->value());
ui->unteretemperatur->setMaximum(ui->oberetemperatur->value());
ui->oberetemperatur->setMinimum(ui->unteretemperatur->value());
ui->unteretemperatur->setSingleStep(ui->aufloesung->value());
ui->oberetemperatur->setSingleStep(ui->aufloesung->value());
werteberechnung(m_PunktListe);
}
main.cpp
#include "dialog.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Dialog w;
w.show();
return a.exec();
}
As noted in the comments, your example is not MCVE. But guessing, there are a couple of clues:
PunktVektor *m_punkte; //This is the mentioned vector
and
datenpunkt test = *m_punkte->begin(); <- //The programm crashs
Unless PunktVector overloads operator->(), your access of the vector should probably be
datenpunkt test = m_punkte->begin(); <- //no dereference (*)
REVISION:
With your updated example, the basic problem is dereferencing the end() iterator, which is a marker and does not refer to an element
datenpunkt test = *m_punkte->begin();
datenpunkt test2 =*m_punkte->end();
*m_punkte->empty();
m_punkte->begin() returns an iterator to the first element (if there is one; the vector could be empty). *( m_punkte->begin() ) accesses the element the iterator refers to.
m_punkte->end() return an iterator marking the end of the container. * (m_punkte->end() ) is undefined.
m_punkte->empty() returns a bool reflecting whether the container is empty or not. *( m_punkte->empty() ) will access either memory location 0 or 1 (both of which are likely to be very bad.
REVISION 2:
When working with containers, it is important to understand how you can hurt yourself. In particular, look here:
void DiagrammWidget::paintEvent(QPaintEvent *event)
{
QPainter myPainter (this);
/*if((m_punkte == 0|| m_punkte->size() == 0)){
myPainter.drawText(100,100,"No Points found!");
return;
}*/
datenpunkt ersterPunkt=*m_punkte->begin();
datenpunkt startPunkt = ersterPunkt;
PunktVektor::iterator it;
for( it = m_punkte->begin()++; it != m_punkte->end(); it++){
datenpunkt p = *it;
myPainter.drawLine(startPunkt.temperatur,startPunkt.laenge,
p.temperatur,p.laenge);
startPunkt = p;
}
}
If you are going to do this: datenpunkt ersterPunkt=*m_punkte->begin();
then you must first check to see of the container is empty.
Container.begin() returns an iterator to the first element of the container. If the container is empty, this iterator is equal to Container.end(). Dereferencing this iterator is a good way to crash your program.
Try refactoring your program like this:
void DiagrammWidget::paintEvent(QPaintEvent *event)
{
QPainter myPainter (this);
if( m_punkte == nullptr || m_punkte->empty() ){
// myPainter.drawText(100,100,"No Points found!");
return;
}
auto startPunkt = m_punkte->begin();
for( auto it = ++(m_punkte->begin()); it != m_punkte->end(); ++it){
auto const& p = *it;
myPainter.drawLine(startPunkt->temperatur,startPunkt->laenge,
p.temperatur,p.laenge);
startPunkt = it;
}
}
You see that this properly protects against your allowed condition of a null pointer or an empty container before making assumptions about the contents of the container. It also uses iterators as intended, rather than making unnecessary copies of the elements of the container.
I get the following error:
QWidget: must construct a QApplication before a widget
It worked 3 days ago, but now it doesn't work anymore, and I don't know why.
What does this error mean? I don't know if I modified something.
Here is my code:
mainwindw.cpp
void MainWindow::on_Statistic_button_clicked(){
s1 =new stat1(this);
s1->show();
}
stat1.cpp
stat1::stat1(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::stat1)
{
ui->setupUi(this);
//mLayout = new QVBoxLayout;
mChart = new OpenChart(this);
setCentralWidget(mChart);
//mLayout->addWidget(mChart);
//mLayout->setMargin(0);
//setLayout(mLayout);
//int num;
QSqlQuery q,q1,q2;
q1.exec("SELECT count(distinct adresse) from abonne");
q1.next();
int n = q1.value(0).toInt();
QString adr[100];
int j = 0;
q.exec("SELECT distinct adresse from abonne");
while(j<n)
{
q.next();
adr[j]=q.value(0).toString();
j++;
}
QMap<QString,int> cartes;
for(j=0;j<n;j++)
{
q2.exec("SELECT count(abonne.id_abonne) FROM abonne,fidels where abonne.id_abonne=fidels.id_abonne and abonne.adresse='"+adr[j]+"'");
q2.next();
int nbr = q2.value(0).toInt();
cartes[adr[j]] = nbr;
}
mChart->setTitle("Cartes/Adresses");
mChart->setTipo(OpenChart::Sectores_2D);
mChart->setTipoleyenda(OpenChart::Circular);
const auto cartesEnd=cartes.end();
for(auto i=cartes.begin(); i!=cartesEnd; ++i){
mChart->addItem(i.key(), i.value());
}
}
stat1::~stat1()
{
delete ui;
}
I'm using QMovie and gif to create explosion after collision. The problem is that my gif is looping over and over, I've checked loopcount status and it returns -1 (infinite). How to display my gif just one time?
#include "Bullet.h"
#include <QTimer>
#include <QGraphicsScene>
#include <QList>
#include "Enemy.h"
#include "Game.h"
#include <typeinfo>
#include "levels.h"
extern Game * game; // there is an external global object called game
int Bullet::killed = 0;
int Bullet::missed = 0;
double Bullet::accurancy = 0;
Bullet::Bullet(QGraphicsItem *parent): QGraphicsPixmapItem(parent){
// draw graphics
setPixmap(QPixmap(":/images/res/images/bullets/bullet.png"));
missed++; // increse missed when bullet is created
movie = new QMovie(":/images/res/images/effects/explosion/64x48.gif");
processLabel = new QLabel;
processLabel->setMovie(movie);
// make/connect a timer to move() the bullet every so often
QTimer * timer = new QTimer(this);
connect(timer,SIGNAL(timeout()),this,SLOT(move()));
// start the timer
timer->start(2);
}
void Bullet::move(){
// get a list of all the items currently colliding with this bullet
QList<QGraphicsItem *> colliding_items = collidingItems();
// if one of the colliding items is an Enemy, destroy both the bullet and the enemy
for (int i = 0, n = colliding_items.size(); i < n; ++i){
if (typeid(*(colliding_items[i])) == typeid(Enemy)){
// increase the score
game->score->increase();
//play explosion animation
movie->start();
movie->setSpeed(180);
processLabel->setAttribute(Qt::WA_NoSystemBackground);
processLabel->setGeometry(QRect(x()-15,y()-15,64,48));
scene()->addWidget(processLabel);
qDebug() << movie->loopCount();
//connect(movie,SIGNAL(finished()),movie,SLOT(stop()));
// remove them from the scene (still on the heap)
scene()->removeItem(colliding_items[i]);
scene()->removeItem(this);
// delete them from the heap to save memory
delete colliding_items[i];
delete this;
killed++;
missed--; // decrese missed if bullet colide with enemy
if((killed+1) % 9 == 0)
{
game->level->Levels::incrementLevels();
game->score->Score::addToSum(); /// TODO
}
//qDebug() << "Already killed: " << killed;
//qDebug() << "Already missed: " << missed;
// return (all code below refers to a non existint bullet)
return;
}
}
// if there was no collision with an Enemy, move the bullet forward
setPos(x(),y()-1);
// if the bullet is off the screen, destroy it
if (pos().y() < 0){
scene()->removeItem(this);
delete this;
}
}
I had the same question and didn't find anything, so here's my solution:
connect the signal "frameChanged(int)" to:
void Bullet::frameChanged_Handler(int frameNumber) {
if(frameNumber == (movie->frameCount()-1)) {
movie->stop();
}
}
If you want to run X times the loop you just have to add a static counter to know how many times you've passed the last frame:
void Bullet::frameChanged_Handler(int frameNumber) {
static int loopCount = 0;
if(frameNumber == (movie->frameCount()-1)) {
loopCount++;
if(loopCount >= MAX_LOOP_TIMES)
movie->stop();
}
}
and of course, connect with this:
connect(movie, SIGNAL(frameChanged(int)), this, SLOT(frameChanged_Handler(int)));
That's it... Hope it can help you.
I am working in Qt 4.7, and I have a QWidget object in my dialog. I need to go through each of the children and extract the current text into a QStringList. Every other object is a QCheckBox, and the rest are QComboBoxes (I would just need the text portion of both). So far the only way I could think of to do this would be to use the children() function to get them as QObject*'s and cast them, like this:
QStringList textlist;
for(int i = 0; i < ui->myWidget->children().size(); i++)
{
if(i % 2 == 0)
{
QCheckBox *q = (QCheckBox *)ui->myWidget->children().at(i);
textlist.append(q->text());
}
else
{
QComboBox *q = (QComboBox *)ui->myWidget->children().at(i);
textlist.append(q->currentText());
}
}
However, when I try to use this, it builds and compiles fine, but then crashes when it's run. I checked and both classes are subclasses (albeit indirectly through QAbstractButton and QWidget) of QObject, which is the type of the objects in the list ui->myWidget->children(), so I feel like they should be able to cast this way. I haven't worked much with this kind of thing before so I'm not sure if there's a better way to do this. If anyone has any ideas, it would be greatly appreciated. Thanks!
UPDATE: So, I can't get the casting to work this way or with the qobject_cast. I HAVE however discovered that I can go from QObject to QWidget, and I think I should be able to go from QWidget to the needed objects with dynamic_cast, but that doesn't work either. Right now I have this:
QStringList textlist;
for(int i = 0; i < ui->myWidget->children().size(); i++)
{
QWidget *qw = qobject_cast<QWidget*>(ui->myWidget->children().at(i)
if(i % 2 == 0)
{
QComboBox *q = dynamic_cast<QComboBox*>(qw);
if(q)
{
textlist.append(q->text());
}
}
else
{
QCheckBox *q = dynamic_cast<QCheckBox*>(qw);
if(q)
{
textlist.append(q->currentText());
}
}
}
If anyone has any ideas, I'd appreciate the help. Thank you.
UPDATE2: I haven't found much online that helps with this still, so I may as well ask as well, if there is anyway to do this WITHOUT casting, i.e. getting the objects directly from the QWidget in their original type, I would really appreciate that as well. I'm not heartset on my current strategy or anything, it was just the only way I could think to do it - I'll take anything that works at this point.
You should think about using qobject_cast. After the cast you should also check if the object is valid.
QStringList textlist;
for(int i = 0; i < ui->myWidget->children().size(); i++)
{
if(i % 2 == 0)
{
QCheckBox *q = qobject_cast<QCheckBox*>(ui->myWidget->children().at(i));
if(q)
textlist.append(q->text());
}
else
{
QComboBox *q = qobject_cast<QComboBox*>(ui->myWidget->children().at(i));
if(q)
textlist.append(q->currentText());
}
}
But this still seems like a bad design to me. The widget class that contains the comboboxes and checkboxes should have a function, that goes through the checkboxes and comboboxes and returns a QStringList. Then you could just call that function.
Here is an example
mywidget.h:
namespace Ui {
class MyWidget;
}
class MyWidget : public QWidget
{
Q_OBJECT
public:
explicit MyWidget(QWidget *parent = 0);
~MyWidget();
QStringList getComboTextList();
QStringList getCheckBoxTextList();
private:
Ui::MyWidget *ui;
QList<QComboBox*> comboList;
QList<QCheckBox*> checkList;
};
mywidget.cpp:
MyWidget::MyWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::MyWidget)
{
ui->setupUi(this);
this->setLayout(new QVBoxLayout);
for(int i = 0; i < 5; i++)
{
QCheckBox *checkBox = new QCheckBox(this);
this->layout()->addWidget(checkBox);
checkBox->setText(QString("Check box #%1").arg(i));
checkList.append(checkBox);
}
for(int i = 0; i < 5; i++)
{
QComboBox *comboBox = new QComboBox(this);
this->layout()->addWidget(comboBox);
comboBox->addItem("Combo box item 1");
comboBox->addItem("Combo box item 2");
comboList.append(comboBox);
}
}
MyWidget::~MyWidget()
{
delete ui;
}
QStringList MyWidget::getComboTextList()
{
QStringList returnList;
for(int i = 0; i < comboList.length(); i++)
{
returnList << comboList[i]->currentText();
}
return returnList;
}
QStringList MyWidget::getCheckBoxTextList()
{
QStringList returnList;
for(int i = 0; i < checkList.length(); i++)
{
returnList << checkList[i]->text();
}
return returnList;
}
Then in your other class you can just call getCheckBoxTextList or getComboTextList like this:
QStringList comboTextList = myWidget->getComboBoxList();
QStringList checkTextList = myWidget->getCheckBoxTextList();
I am working in Qt 4.7, and I have a QWidget object in my dialog. I need to go through each of the children and extract the current text into a QStringList. Every other object is a QCheckBox, and the rest are QComboBoxes (I would just need the text portion of both). So far the only way I could think of to do this would be to use the children() function to get them as QObject*'s and cast them, like this:
QStringList textlist;
for(int i = 0; i < ui->myWidget->children().size(); i++)
{
if(i % 2 == 0)
{
QCheckBox *q = (QCheckBox *)ui->myWidget->children().at(i);
textlist.append(q->text());
}
else
{
QComboBox *q = (QComboBox *)ui->myWidget->children().at(i);
textlist.append(q->currentText());
}
}
However, when I try to use this, it builds and compiles fine, but then crashes when it's run. I checked and both classes are subclasses (albeit indirectly through QAbstractButton and QWidget) of QObject, which is the type of the objects in the list ui->myWidget->children(), so I feel like they should be able to cast this way. I haven't worked much with this kind of thing before so I'm not sure if there's a better way to do this. If anyone has any ideas, it would be greatly appreciated. Thanks!
UPDATE: So, I can't get the casting to work this way or with the qobject_cast. I HAVE however discovered that I can go from QObject to QWidget, and I think I should be able to go from QWidget to the needed objects with dynamic_cast, but that doesn't work either. Right now I have this:
QStringList textlist;
for(int i = 0; i < ui->myWidget->children().size(); i++)
{
QWidget *qw = qobject_cast<QWidget*>(ui->myWidget->children().at(i)
if(i % 2 == 0)
{
QComboBox *q = dynamic_cast<QComboBox*>(qw);
if(q)
{
textlist.append(q->text());
}
}
else
{
QCheckBox *q = dynamic_cast<QCheckBox*>(qw);
if(q)
{
textlist.append(q->currentText());
}
}
}
If anyone has any ideas, I'd appreciate the help. Thank you.
UPDATE2: I haven't found much online that helps with this still, so I may as well ask as well, if there is anyway to do this WITHOUT casting, i.e. getting the objects directly from the QWidget in their original type, I would really appreciate that as well. I'm not heartset on my current strategy or anything, it was just the only way I could think to do it - I'll take anything that works at this point.
You should think about using qobject_cast. After the cast you should also check if the object is valid.
QStringList textlist;
for(int i = 0; i < ui->myWidget->children().size(); i++)
{
if(i % 2 == 0)
{
QCheckBox *q = qobject_cast<QCheckBox*>(ui->myWidget->children().at(i));
if(q)
textlist.append(q->text());
}
else
{
QComboBox *q = qobject_cast<QComboBox*>(ui->myWidget->children().at(i));
if(q)
textlist.append(q->currentText());
}
}
But this still seems like a bad design to me. The widget class that contains the comboboxes and checkboxes should have a function, that goes through the checkboxes and comboboxes and returns a QStringList. Then you could just call that function.
Here is an example
mywidget.h:
namespace Ui {
class MyWidget;
}
class MyWidget : public QWidget
{
Q_OBJECT
public:
explicit MyWidget(QWidget *parent = 0);
~MyWidget();
QStringList getComboTextList();
QStringList getCheckBoxTextList();
private:
Ui::MyWidget *ui;
QList<QComboBox*> comboList;
QList<QCheckBox*> checkList;
};
mywidget.cpp:
MyWidget::MyWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::MyWidget)
{
ui->setupUi(this);
this->setLayout(new QVBoxLayout);
for(int i = 0; i < 5; i++)
{
QCheckBox *checkBox = new QCheckBox(this);
this->layout()->addWidget(checkBox);
checkBox->setText(QString("Check box #%1").arg(i));
checkList.append(checkBox);
}
for(int i = 0; i < 5; i++)
{
QComboBox *comboBox = new QComboBox(this);
this->layout()->addWidget(comboBox);
comboBox->addItem("Combo box item 1");
comboBox->addItem("Combo box item 2");
comboList.append(comboBox);
}
}
MyWidget::~MyWidget()
{
delete ui;
}
QStringList MyWidget::getComboTextList()
{
QStringList returnList;
for(int i = 0; i < comboList.length(); i++)
{
returnList << comboList[i]->currentText();
}
return returnList;
}
QStringList MyWidget::getCheckBoxTextList()
{
QStringList returnList;
for(int i = 0; i < checkList.length(); i++)
{
returnList << checkList[i]->text();
}
return returnList;
}
Then in your other class you can just call getCheckBoxTextList or getComboTextList like this:
QStringList comboTextList = myWidget->getComboBoxList();
QStringList checkTextList = myWidget->getCheckBoxTextList();