views:

155

answers:

1

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;
}

========================================================================================

+1  A: 

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.

Jeremy Friesner