I created so much qpushbutton
to represent the seat in the cinema. After user buy the seats I made these seats disabled. all I want to do is to see previously disabled button disabled. I saved this disabled button 开发者_如何转开发to a txt file and read their name but I could not assign it as my widget Qpushbuttons. Is there a way to solve it?
This is not as much a button issue as it is a data structure issue. You should somehow connect your buttons/seats to a data structure which aids in the bookkeeping of available and reserved seats. Once you close the program, you write out the data to a file or database, which you can subsequently read again when you open your application. You can then disable the buttons again of those seats which are reserved.
I've made some quick example with Qt, I hope this will help you:
// list of all seats in order (true means seat is taken, false seat is still free)
QList<bool> seats;
// set some test values
seats.append(true);
seats.append(true);
seats.append(false);
seats.append(true);
// file where the seats will be stored
QFile file("seats.dat");
// save to file
file.open(QIODevice::WriteOnly);
QDataStream out(&file);
out << seats;
file.close();
// remove all seats (just for testing)
seats.clear();
// read from file
file.open(QIODevice::ReadOnly);
QDataStream in(&file);
in >> seats;
file.close();
// simple debug output off all seats
qDebug() << seats;
// you could set the buttons enabled state like this
QList<QPushButton*> buttons; // list of your buttons in the same order as the seat list of course
for (int i = 0; i < seats.count(); ++i)
buttons[i]->setEnabled(!seats.at(i)); // disables all seats which are already taken
This is off course just a simple solution using a QDataStream to serialize the complete List of seats, but you can play around with this if you are new to Qt/C++
精彩评论