QTimer &选择使用。需要让拍卖时间发挥作用
目前我在使用 QTimer 时遇到问题。我试图让每个“产品”都有一个计时器,这样它会让我的“处理程序”知道产品的拍卖时间已经结束。我遇到的问题是,从我的测试来看,它似乎没有打印“计时器已启动”。
这是我的套接字编程项目的一部分。我用select是因为我还没学多线程。所以如果有人可以给我提示。当客户端发送消息时,我的 HandleTCPClient 会从我的服务器调用。提前致谢。
来自我的 HandleTCPClient 的Product.cpp
#include <string>
using std::string;
#include <iostream>
using std::cout;
#include "Product.h"
Product::Product()
{
seller = "";
itemName = "";
price = 0.00;
min = 0.00;
buyingPrice = 0.00;
time = 0;
description = "";
highestBidder = "None";
currentBid = 0.00;
timer = new QTimer( this );
connect( timer, SIGNAL(timeout()), this, SLOT(setProductToSold()) );
}
void Product::startTimer()
{
cout << " Timer Started " << endl; <<< When called this isn't printed
timer->start( 2000, TRUE ); // 2 seconds single-shot timer
}
void Product::setHandler(Handler *h)
{
handler = h;
}
void Product::setProductToSold()
{
cout << " Item auction over" << endl;
}
:
...
vector <Account> account;
vector <Product *> product;
...
product = checkUser(product,&tempProduct,clntSocket); // check to see if the user is logged in, else can't sell product
output = checkUserSell(clntSocket); // generate a string to see if user is logged in or not
tempProduct.startTimer();
// Start the Timer!!!
其中 :
// Push product onto products list
vector <Product *> checkUser(vector <Product *> products, Product * temp, int clntSocket) // generate a new product: check to see if user is logged in or not (2) sell product
{
for (int i=0; i<account.size(); i++)
{
if (account[i].socket == clntSocket) // user is logged in!
{
temp->seller = account[i].name;
products.push_back(temp);
break;
}
}
return products;
}
和 ...
string checkUserSell(int clntSocket) // generate a string: check to see if user is logged in or not (2) sell product
{
string output = "2\n<You must first log in if you would like to sell your products>\n<<<TRANSACTION CANCELLED>>>\n";
for (int i=0; i<account.size(); i++)
{
if (account[i].socket == clntSocket) // user is logged in!
{
output = "1\n<Your product has been put up for Auction>\n";
break;
}
}
return output;
}
======================================= =================================================== =
Currently I am having issues with QTimer. I am trying to make each "Product" have a timer and so it will let my "Handler" know that the auction time of the product has ended. The problem that I am having is that from my test, it doesn't seem to print "Timer started".
This is part of my socket programming project. I use select because I haven't learned multithreading yet. So if anyone can give me tips. My HandleTCPClient is called from my server when a client sends a message. Thanks in advance.
Product.cpp
#include <string>
using std::string;
#include <iostream>
using std::cout;
#include "Product.h"
Product::Product()
{
seller = "";
itemName = "";
price = 0.00;
min = 0.00;
buyingPrice = 0.00;
time = 0;
description = "";
highestBidder = "None";
currentBid = 0.00;
timer = new QTimer( this );
connect( timer, SIGNAL(timeout()), this, SLOT(setProductToSold()) );
}
void Product::startTimer()
{
cout << " Timer Started " << endl; <<< When called this isn't printed
timer->start( 2000, TRUE ); // 2 seconds single-shot timer
}
void Product::setHandler(Handler *h)
{
handler = h;
}
void Product::setProductToSold()
{
cout << " Item auction over" << endl;
}
From my HandleTCPClient:
...
vector <Account> account;
vector <Product *> product;
...
product = checkUser(product,&tempProduct,clntSocket); // check to see if the user is logged in, else can't sell product
output = checkUserSell(clntSocket); // generate a string to see if user is logged in or not
tempProduct.startTimer();
// Start the Timer!!!
Where :
// Push product onto products list
vector <Product *> checkUser(vector <Product *> products, Product * temp, int clntSocket) // generate a new product: check to see if user is logged in or not (2) sell product
{
for (int i=0; i<account.size(); i++)
{
if (account[i].socket == clntSocket) // user is logged in!
{
temp->seller = account[i].name;
products.push_back(temp);
break;
}
}
return products;
}
and ...
string checkUserSell(int clntSocket) // generate a string: check to see if user is logged in or not (2) sell product
{
string output = "2\n<You must first log in if you would like to sell your products>\n<<<TRANSACTION CANCELLED>>>\n";
for (int i=0; i<account.size(); i++)
{
if (account[i].socket == clntSocket) // user is logged in!
{
output = "1\n<Your product has been put up for Auction>\n";
break;
}
}
return output;
}
========================================================================================
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请记住,Qt 的事件循环是单线程的:如果您在主线程内的 select() 中阻塞,那么 Qt 的事件循环在 select() 返回之前无法运行,因此任何挂起的 QTimer 超时事件都不会被执行。可能的解决方案:不要在 select() 中阻塞,而是使用 Qt 的 QSocket 类(等)来实现网络(以便将网络事件多路复用集成到 Qt 事件循环中)...或者运行基于 select() 的事件循环与 Qt 事件循环线程分开的线程。第一个解决方案更简单。
Keep in mind that Qt's event loop is single threaded: If you are blocking in select() inside the main thread, then Qt's event loop can't run until select() returns, and so any pending QTimer timeout events won't be executed. Possible solutions: instead of blocking in select(), use Qt's QSocket class (etc) to implement your networking (so that network event multiplexing is integrated into the Qt event loop) ... or run your select()-based event loop in a separate thread from the Qt event loop thread. The first solution is simpler.