0

QVector<QPushButton*> buttonList; global variable
void SeatReservImpl::selectSeat(){ select seat from the gui which are pushbutton

QPushButton* clickedButton= (QPushButton*)(sender()); select the clicked button
buttonList.insert(clickedButton); add to the list but gives the error:no matching function for call to QVector::insert(QPushButton*&)

}

void SeatReservImpl::buyTicket(){ buy selected seats when pressed buy button
for ( pb=buttonList.first(); pb != 0; pb=buttonList.next() ) { pb->setPalette(QPalette(QColor(255,0,0))); change the color of the buttons
}
The problem is; the code is wrong and do not know how to fix them and the thing I want to do is how can I add the clicked pushbuttons to a QVector or Qlist and how to use the list or vector and reach the saved button inside the list

Edit:

Here is my code so far:

#include "SeatReservImpl.h"
#include <qmessagebox.h>
#include <stdio.h>
#include <SeatReserv.h>
#include <qcheckbox.h>
#include <qpushbutton.h>
#include <qbuttongroup.h>
#include <qbutton.h>
#include <qobject.h>
#include <qwidget.h>
#include <qcolor.h>
#include <qpalette.h>
#include <string>
#include <stdlib.h>
#include <iostream>
#include <qlist.h>
#include <qvector.h>
using namespace std;

QString slcSeat[10];
int i;
QList<QPushButton*> buttonList;

void SeatReservImpl::selectSeat()
{

QString slcSeat[10];

QPushButton* clickedButton = (QPushButton*)(sender());
QString buttName=clickedButton->name();
buttonList.append(clickedButton);
slcSeat[i]=buttName;
i++;
clickedButton->setPalette(QPalette(QColor(174,0,255)));

}

void SeatReservImpl::buyTicket(){

FILE *pfile;
char status[2];
int m;
int flag=0;
QString buttName,buttName1;
for(m=0;m<i;m++){
    buttName=slcSeat[m];
    system("wget -O status.txt http://embsys.heliohost.org/find.php?Seat="+buttName);
    pfile=fopen("status.txt","r+");       
    fgets(status,sizeof status,pfile);
    if(strcmp(status,"0")!=0)
        flag=1;
    }
if(flag){
    QMessageBox::information( this, "", "You Have the Seats", QMessageBox::Ok );
    QListIterator<QPushButton*> iter(buttonList);
    while (iter.hasNext()) {
        QPushButton *pb = iter.next();
        pb->setPalette(QPalette(QColor(255,0,0)));
            system("wget http://embsys.heliohost.org/buy.php?Seat="+buttName);
        }
}else{
    QMessageBox::information( this, "","\t\t SORRY!!!\n One of The Seats Has Been Already Sold",    QMessageBox::Ok );
    }

    i=0;

}

But I'm getting compiler errors:

And the errors are;

SeatReservImpl.cpp: In member function ‘virtual void SeatReservImpl::selectSeat()’:
SeatReservImpl.cpp:32: error: no matching function for call to ‘QList::append(QPushButton*&)’ ../qtopia-2.2.0-FriendlyARM/qt2/include/qlist.h:61: note: candidates are: void QList::append(const type*) [with type = QPushButton*]
SeatReservImpl.cpp: In member function ‘virtual void SeatReservImpl::buyTicket()’:
SeatReservImpl.cpp:58: error: ‘class QListIterator’ has no member named ‘hasNext’
SeatReservImpl.cpp:59: error: ‘class QListIterator’ has no member named ‘next’
Mat
  • 202,337
  • 40
  • 393
  • 406
Cengaver
  • 3
  • 2
  • 4
  • see updated answer. if that's still not good, please edit **your question** and post the exact version of Qt you are using, and the version of your compiler. – Mat Apr 24 '11 at 06:36

3 Answers3

1

Use something like this:

QList<QPushButton*> buttonList;


// add an item:
buttonList.append(clickedButton);

// iterate over all items
QListIterator<QPushButton*> iter(buttonList);
while (iter.hasNext()) {
  QPushButton *pb = iter.next();
  ...
}

There are lots of other ways, and QList is not necessarily the most appropriate container class. Please read the Container Classes reference documentation for information about all the different options you have.

Edit:

If you're using a very old version of Qt for some reason, the QList class (and the iterator) don't work like that. They are a bit strange. I don't have anything to test compile this, so YYMV. Looking at the docs for QList in Qt 2.3:

QList<QPushButton> buttonList; // note: not QPushButton*

// add an item:
buttonList.append(clickedButton); // same as above

// iterate over all items
QListIterator<QPushButton> iter(buttonList);  // note: not QPushButton*
for ( ; iter.current(); ++iter ) {
  QPushButton *pb = iter.current();
  ...
}
Community
  • 1
  • 1
Mat
  • 202,337
  • 40
  • 393
  • 406
  • I disturbed you so much but it gives errors no matching function for call to `QList::append(QPushButton*&)` error: `class QListIterator` has no member named `hasNext` and `next` I think there are errors becouse of the versions I am SORRY Again – Cengaver Apr 23 '11 at 16:55
  • I wrote all my code and all the errors. Thank you for all your help and interest. – Cengaver Apr 24 '11 at 03:42
0

As a supplement to the answer above, it is recommended to do:

QPushButton* clickedButton= dynamic_cast<QPushButton*>(sender());

and check whether the cast was sucessful instead of just:

QPushButton* clickedButton= (QPushButton*)(sender());
snoofkin
  • 8,725
  • 14
  • 49
  • 86
0
#include "SeatReservImpl.h"
#include <qmessagebox.h>
#include <stdio.h>
#include <SeatReserv.h>
#include <qcheckbox.h>
#include <qpushbutton.h>
#include <qbuttongroup.h>
#include <qbutton.h>
#include <qobject.h>
#include <qwidget.h>
#include <qcolor.h>
#include <qpalette.h>
#include <string>
#include <stdlib.h>
#include <iostream>
#include <qlist.h>
#include <qvector.h>
using namespace std;

QString slcSeat[10];
int i;
QList<QPushButton*> buttonList;

void SeatReservImpl::selectSeat()
{

QString slcSeat[10];

QPushButton* clickedButton = (QPushButton*)(sender());
QString buttName=clickedButton->name();
buttonList.append(clickedButton);
slcSeat[i]=buttName;
i++;
clickedButton->setPalette(QPalette(QColor(174,0,255)));

}

void SeatReservImpl::buyTicket(){

FILE *pfile;
char status[2];
int m;
int flag=0;
QString buttName,buttName1;
for(m=0;m<i;m++){
    buttName=slcSeat[m];
    system("wget -O status.txt http://embsys.heliohost.org/find.php?Seat="+buttName);
    pfile=fopen("status.txt","r+");       
    fgets(status,sizeof status,pfile);
    if(strcmp(status,"0")!=0)
        flag=1;
    }
if(flag){
    QMessageBox::information( this, "", "You Have the Seats", QMessageBox::Ok );
    QListIterator<QPushButton*> iter(buttonList);
    while (iter.hasNext()) {
        QPushButton *pb = iter.next();
        pb->setPalette(QPalette(QColor(255,0,0)));
            system("wget http://embsys.heliohost.org/buy.php?Seat="+buttName);
        }
}else{
    QMessageBox::information( this, "","\t\t SORRY!!!\n One of The Seats Has Been Already Sold",    QMessageBox::Ok );
    }

    i=0;

}

> Blockquote

And the errors are;

SeatReservImpl.cpp: In member function ‘virtual void SeatReservImpl::selectSeat()’:
SeatReservImpl.cpp:32: error: no matching function for call to ‘QList::append(QPushButton*&)’ ../qtopia-2.2.0-FriendlyARM/qt2/include/qlist.h:61: note: candidates are: void QList::append(const type*) [with type = QPushButton*]
SeatReservImpl.cpp: In member function ‘virtual void SeatReservImpl::buyTicket()’:
SeatReservImpl.cpp:58: error: ‘class QListIterator’ has no member named ‘hasNext’
SeatReservImpl.cpp:59: error: ‘class QListIterator’ has no member named ‘next’

Cengaver
  • 3
  • 2
  • 4