如何使用Qvector <qpushbutton> </qpushbutton>

时间:2011-04-23 16:23:45

标签: qt

QVector<QPushButton*> buttonList;全局变量
void SeatReservImpl::selectSeat(){从gui选择座位按钮

QPushButton* clickedButton= (QPushButton*)(sender());选择点击的按钮
buttonList.insert(clickedButton);添加到列表但给出错误:没有匹配函数来调用QVector :: insert(QPushButton *&amp;)

}

按下购买按钮时,

void SeatReservImpl::buyTicket(){购买所选座位 for ( pb=buttonList.first(); pb != 0; pb=buttonList.next() ) { pb->setPalette(QPalette(QColor(255,0,0)));更改按钮的颜色
    }
问题是;代码是错误的,不知道如何修复它们,我想要做的是如何将点击的按钮添加到QVector或Qlist以及如何使用列表或向量并到达列表中保存的按钮

编辑:

到目前为止,这是我的代码:

#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;

}

但我收到了编译错误:

错误是;

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’

3 个答案:

答案 0 :(得分:1)

使用类似的东西:

QList<QPushButton*> buttonList;


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

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

还有很多其他方法,QList不一定是最合适的容器类。有关您拥有的所有不同选项的信息,请阅读Container Classes参考文档。

编辑:

如果您出于某种原因使用了非常旧的版本的Qt,那么QList类(和迭代器)就不会那样工作了。他们有点奇怪。我没有任何东西可以测试这个,所以YYMV。查看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();
  ...
}

答案 1 :(得分:0)

作为上述答案的补充,建议:

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

并检查演员表是否成功而不仅仅是:

QPushButton* clickedButton= (QPushButton*)(sender());

答案 2 :(得分: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

错误是;

SeatReservImpl.cpp:在成员函数'virtual void SeatReservImpl :: selectSeat()'中:
SeatReservImpl.cpp:32:错误:没有匹配函数来调用'QList :: append(QPushButton *&amp;)' ../qtopia-2.2.0-FriendlyARM/qt2/include/qlist.h:61:注意:候选人是:void QList :: append(const type *)[with type = QPushButton *]
SeatReservImpl.cpp:在成员函数'virtual void SeatReservImpl :: buyTicket()'中:
SeatReservImpl.cpp:58:错误:'class QListIterator'没有名为'hasNext'的成员
SeatReservImpl.cpp:59:错误:'class QListIterator'没有名为'next'的成员