void Widget::usrLeft(QString usrname, QString time) { int rowNum = ui->usrTblWidget->findItems(usrname, Qt::MatchExactly).first()->row(); ui->usrTblWidget->removeRow(rowNum); ui->msgBrowser->setTextColor(Qt::gray); ui->msgBrowser->setCurrentFont(QFont("Times New Roman", 10)); ui->msgBrowser->append(tr("%1 于 %2 离开!").arg(usrname).arg(time)); ui->usrNumLbl->setText(tr(" 在 线 人 数 : %1").arg(ui->usrTblWidget->rowCount())); }
时间: 2024-04-03 15:34:16 浏览: 10
这段代码是在一个名为 Widget 的类中,定义了一个 usrLeft 函数。这个函数接受两个参数,分别是 usrname 和 time,它的作用是将 usrname 对应的用户从用户表格控件中移除,并在消息浏览器中显示该用户离开的消息。同时,它还更新了在线用户数量的标签。具体实现中,通过 findItems 函数找到 usrname 对应的单元格,然后调用 removeRow 函数将该行从表格中移除。接着,使用 append 函数在消息浏览器中添加一行文字,其中使用 tr 函数进行文本的本地化处理。最后,更新在线用户数量的标签。
相关问题
Widget::Widget(QWidget *parent, QString usrname) : QWidget(parent), ui(new Ui::Widget) { ui->setupUi(this); uName = usrname; udpSocket = new QUdpSocket(this); port = 23232; udpSocket->bind(port, QUdpSocket::ShareAddress|QUdpSocket::ReuseAddressHint); connect(udpSocket,SIGNAL(readyRead()), this, SLOT(processPendingDatagrams()));//随时接收来自其他用户的UDP广播消息 sndMsg(UsrEnter); srv = new Server(this); connect(srv, SIGNAL(sndFileName(QString)), this, SLOT(getFileName(QString))); } Widget::~Widget() { delete ui; } //处理新用户加入 void Widget::usrEnter(QString usrname, QString ipaddr) { bool isEmpty = ui->usrTblWidget->findItems(usrname, Qt::MatchExactly).isEmpty(); if (isEmpty) { QTableWidgetItem *usr = new QTableWidgetItem(usrname); QTableWidgetItem *ip = new QTableWidgetItem(ipaddr); ui->usrTblWidget->insertRow(0); ui->usrTblWidget->setItem(0, 0, usr); ui->usrTblWidget->setItem(0, 1, ip); ui->msgBrowser->setTextColor(Qt::gray); ui->msgBrowser->setCurrentFont(QFont("Times New Roman", 10)); ui->msgBrowser->append(tr("%1 在线!").arg(usrname)); ui->usrNumLbl->setText(tr("在线人数: 1%").arg(ui->usrTblWidget->rowCount())); sndMsg(UsrEnter);//在线的各个端口也要告诉新加入的断点它们自己的用户信息 } } void Widget::usrLeft(QString usrname, QString time) { int rowNum = ui->usrTblWidget->findItems(usrname, Qt::MatchExactly).first()->row(); ui->usrTblWidget->removeRow(rowNum); ui->msgBrowser->setTextColor(Qt::gray); ui->msgBrowser->setCurrentFont(QFont("Times New Roman", 10)); ui->msgBrowser->append(tr("%1 于 %2 离开!").arg(usrname).arg(time)); ui->usrNumLbl->setText(tr(" 在 线 人 数 : %1").arg(ui->usrTblWidget->rowCount())); }
这段代码是一个Qt的Widget类的成员函数,主要实现了处理新用户加入和用户离开的功能。具体来说,当一个新用户加入聊天室时,会在聊天室的用户列表中添加该用户的用户名和IP地址,并向聊天室中的其他用户发送UDP广播消息通知他们有新用户加入了聊天室。当一个用户离开聊天室时,会从用户列表中移除该用户的信息,并向聊天室中的其他用户发送UDP广播消息通知他们该用户已经离开聊天室。
在代码中,变量uName表示当前用户的用户名,udpSocket表示用于接收UDP广播消息的套接字,srv表示用于处理文件传输的服务器对象。在构造函数中,首先绑定了UDP套接字,并且连接了readyRead()信号与processPendingDatagrams()槽,以便随时接收来自其他用户的UDP广播消息。然后调用了sndMsg函数向聊天室中的其他用户发送UDP广播消息,表示当前用户已经加入聊天室。最后创建了一个服务器对象,连接了其sndFileName信号与getFileName槽,以便在文件传输时获取文件名。
在usrEnter函数中,首先判断该用户是否已经在用户列表中,如果不在,则创建一个新的用户表项,并在用户列表中插入该项。然后向聊天室中的其他用户发送UDP广播消息,通知他们有新用户加入了聊天室。最后将在线人数显示在用户界面上。
在usrLeft函数中,首先找到该用户在用户列表中的行数,并从用户列表中移除该用户的信息。然后向聊天室中的其他用户发送UDP广播消息,通知他们该用户已经离开聊天室。最后将在线人数显示在用户界面上。
void Widget::on_btnCal_clicked() { int num = ui->editNum->text().toInt(); float price = ui->editPrice->text().toFloat(); float total = num*price; QString str = QString::setNum(total,'g',2); }为什么会出现without boject的报错
这个报错可能是因为您在使用 `QString::setNum()` 函数时,没有指定该函数的调用对象,即没有使用 `str.` 来调用该函数。您可以将代码修改为以下形式,即在 `setNum()` 函数前加上 `str.`:
```
void Widget::on_btnCal_clicked() {
int num = ui->editNum->text().toInt();
float price = ui->editPrice->text().toFloat();
float total = num * price;
QString str = QString();
str.setNum(total, 'g', 2);
// 这里可以添加代码将结果输出或者展示给用户
}
```
这样修改后,应该就不会再出现 "without boject" 的报错了。