如何查看重新运行程序时禁用的禁用按钮
问题描述:
我创建了很多qpushbutton
来代表电影院的座位。用户购买座位后,我禁用了这些座位。我想要做的就是查看以前禁用的按钮。我将这个禁用的按钮保存到一个txt文件并读取他们的名字,但我不能将它分配为我的小部件Qpushbuttons。有没有办法解决它?如何查看重新运行程序时禁用的禁用按钮
答
这不是一个按钮问题,因为它是一个数据结构问题。您应该以某种方式将您的按钮/座位连接到一个数据结构,该数据结构有助于可用座位和预留座位的簿记。关闭程序后,将数据写入文件或数据库,随后在打开应用程序时可以再次读取它们。然后,您可以再次禁用那些保留的座位的按钮。
答
我做了Qt一些简单的例子,我希望这将帮助你:
// 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
这偏离航线只使用操作QDataStream序列化的席位完整列表一个简单的解决方案,但你可以如果您是Qt/C++的新手,请使用此功能。
我从文本文件中读取单击的按钮名称,但无法指定为qpushbutton或qpushbutton名称。它可能被保存为一个qpushbutton,但我不知道该怎么做。 – Cengaver 2011-05-13 19:44:45
为什么让自己很难?怎么样一个非常简单(或简单化)的解决方案。假设你有100个座位。创建两个向量。一个包含100个布尔值(true:保留,false:可用),另一个包含100个(引用)按钮。座位3是预留的?然后在一个矢量中将布尔3设置为true,并在另一个矢量中禁用按钮3。你保存和加载的是布尔值(或者你希望使用的任何数据类型)。不要说这是做它的唯一/最好的方式,但它应该让你开始。 – Bart 2011-05-13 19:58:04
你是对的,但我不知道该怎么做。 :)对不起,我打扰你了。 – Cengaver 2011-05-13 20:08:45