从Qt调用C++函数(插槽不起作用)
Calling a C++ function from Qt (slot does not work)
我用c++编写了一个客户端-服务器程序。现在我必须使用QT来创建界面。我已经创建了一个登录界面。当我单击登录按钮时,它应该调用sendmessage函数,该函数将用户名和密码发送到服务器(也用C++编写),服务器会对其进行验证,并将true或false发送回sendmessage函数。如果是真的,该窗口应该关闭并打开一个新窗口,否则它应该再次显示登录窗口,以便用户可以重试。现在,如何实现这一点,循环,以及是否可以使用信号和槽来调用c++函数。关于如何进行,一些想法会很好
以下是我的代码:
Log::Log(QWidget *parent)
:QWidget(parent){
QGridLayout *grid = new QGridLayout(this);
grid->setSpacing(20);
QLabel *username = new QLabel("Username", this);
grid->addWidget(username, 0, 0, 1, 1);
edt1 = new QLineEdit(this);
grid->addWidget(edt1, 0, 1, 1, 3);
QLabel *password = new QLabel("Password", this);
grid->addWidget(password, 1, 0, 1, 1);
edt2 = new QLineEdit(this);
grid->addWidget(edt2, 1, 1, 1, 3);
QPushButton *login = new QPushButton("Login", this);
grid->addWidget(login, 3, 4, 1, 1);
setLayout(grid);
connect(login, SIGNAL(clicked()), this, SLOT(send_message()));
}
//Other code
bool send_message(){
buff = edt1->text() + edt2->text();
write(socket, buff_send, 256); //Sends to the server
read(socket, buff_recv, 256); //Gets the reply from the server
if(buff_recv==true){
loginwin.close();
return true}
else return false;
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Login logwin;
logwin.move(300, 300);
logwin.setWindowTitle("LOGIN");
window.show();
}
我知道这是完全错误的。但我希望你明白我想说的,并给出一些如何做到这一点的技巧。
只需在头文件中注册一个Qt插槽。
class Log {
...
Q_SLOTS:
void sendMessage ();
}
并在实现中实现时隙,例如通过:
Log::sendMessage() {
send_message();
}
不要忘记将connect
语句更改为仅sendMessage()
函数,而不是send_message
函数。
相关文章:
- 我的神经网络不起作用 [XOR 问题]
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- C++为什么尽管我调用了void函数,它却不起作用
- 为什么在保护模式下继承升级不起作用
- 循环在计数器中不起作用
- 在其他文件中创建类时在 c++ 项目中不起作用
- Visual studio代码重构似乎不起作用(例如,重命名符号-f2)
- 为什么二进制搜索在我的测试中不起作用
- 我的代码中有错误吗?使用BGI图形的C++代码对我不起作用
- 为什么 const std::p air<K,V>& 在 std::map 上基于范围的 for 循环不起作用?
- 带有指定长度字符* 参数的 std::regex_search 在 VS2017 中不起作用?
- Bjarne Stroustrup Book - std_lib_facilities.h - 不起作用(未知类型名称)
- Qt信号和插槽如果从QRunnable或其他线程调用,则不起作用
- QT5 C 信号到QML插槽不起作用
- 连接另一个在Qt中不起作用的类的插槽
- Qt 信号和插槽在更新 QTableView 时不起作用
- QML C 信号插槽不起作用
- 从Qt调用C++函数(插槽不起作用)
- 带有返回值的信号/插槽不起作用