Drawer::Drawer(QWidget *parent, Qt::WindowFlags f) :QToolBox(parent, f) { setWindowTitle(tr("Myself QQ 2013")); //设置主窗口的标题 setWindowIcon(QPixmap(":/image/qq.png")); //设置主窗体标题栏图标 toolBtn1 = new QToolButton; toolBtn1->setText(tr("道枝小尧")); toolBtn1->setIcon(QPixmap(":/image/spqy.png")); toolBtn1->setIconSize(QPixmap(":/image/spqy.png").size()); toolBtn1->setAutoRaise(true); toolBtn1->setToolButtonStyle(Qt::ToolButtonTextBesideIcon); connect(toolBtn1, SIGNAL(clicked()), this, SLOT(showChatWidget1())); toolBtn2 = new QToolButton; toolBtn2->setText(tr("忆梦如澜")); toolBtn2->setIcon(QPixmap(":/image/ymrl.png")); toolBtn2->setIconSize(QPixmap(":/image/ymrl.png").size()); toolBtn2->setAutoRaise(true); toolBtn2->setToolButtonStyle(Qt::ToolButtonTextBesideIcon); connect(toolBtn2, SIGNAL(clicked()), this, SLOT(showChatWidget2())); toolBtn3 = new QToolButton; toolBtn3->setText(tr("出版人")); toolBtn3->setIcon(QPixmap(":/image/qq.png")); toolBtn3->setIconSize(QPixmap(":/image/qq.png").size()); toolBtn3->setAutoRaise(true); toolBtn3->setToolButtonStyle(Qt::ToolButtonTextBesideIcon); connect(toolBtn3, SIGNAL(clicked()), this, SLOT(showChatWidget3())); toolBtn4 = new QToolButton; toolBtn4->setText(tr("Cherry")); toolBtn4->setIcon(QPixmap(":/image/Cherry.png")); toolBtn4->setIconSize(QPixmap(":/image/Cherry.png").size()); toolBtn4->setAutoRaise(true); toolBtn4->setToolButtonStyle(Qt::ToolButtonTextBesideIcon); connect(toolBtn4, SIGNAL(clicked()), this, SLOT(showChatWidget4())); toolBtn5 = new QToolButton; toolBtn5->setText(tr("淡然")); toolBtn5->setIcon(QPixmap(":/image/dr.png")); toolBtn5->setIconSize(QPixmap(":/image/dr.png").size()); toolBtn5->setAutoRaise(true); toolBtn5->setToolButtonStyle(Qt::ToolButtonTextBesideIcon); connect(toolBtn5, SIGNAL(clicked()), this, SLOT(showChatWidget5())); toolBtn6 = new QToolButton; toolBtn6->setText(tr("娇娇")); toolBtn6->setIcon(QPixmap(":/image/jj.png")); toolBtn6->setIconSize(QPixmap(":/image/jj.png").size()); toolBtn6->setAutoRaise(true); toolBtn6->setToolButtonStyle(Qt::ToolButtonTextBesideIcon); connect(toolBtn6, SIGNAL(clicked()), this, SLOT(showChatWidget6())); toolBtn7 = new QToolButton; toolBtn7->setText(tr("洛水无痕")); toolBtn7->setIcon(QPixmap(":/image/lswh.png")); toolBtn7->setIconSize(QPixmap(":/image/lswh.png").size()); toolBtn7->setAutoRaise(true); toolBtn7->setToolButtonStyle(Qt::ToolButtonTextBesideIcon); connect(toolBtn7, SIGNAL(clicked()), this, SLOT(showChatWidget7()));改为连接网络数据库
时间: 2024-04-07 13:28:32 浏览: 64
将以上代码改为连接网络数据库需要进行以下步骤:
1. 首先需要创建一个数据库,并在其中创建一个名为“users”的表,包含“id”、“name”和“avatar”三个字段,分别代表用户的ID、昵称和头像路径。
2. 然后需要使用Qt提供的QSqlDatabase类来连接数据库,可以使用以下代码:
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost"); //数据库服务器地址
db.setPort(3306); //数据库端口号
db.setDatabaseName("mydatabase"); //数据库名称
db.setUserName("root"); //数据库用户名
db.setPassword("123456"); //数据库密码
bool ok = db.open();
if (ok) {
qDebug() << "数据库连接成功!";
} else {
qDebug() << "数据库连接失败!";
}
3. 连接成功后,可以通过QSqlQuery类来执行SQL语句,获取用户信息并显示在界面上。以下是示例代码:
QSqlQuery query;
query.exec("SELECT * FROM users");
while (query.next()) {
QString name = query.value("name").toString();
QString avatar = query.value("avatar").toString();
QToolButton *btn = new QToolButton;
btn->setText(name);
btn->setIcon(QPixmap(avatar));
btn->setIconSize(QPixmap(avatar).size());
btn->setAutoRaise(true);
btn->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
connect(btn, SIGNAL(clicked()), this, SLOT(showChatWidget()));
//将按钮添加到界面上
this->addItem(btn, "好友列表");
}
阅读全文