Compile a piece of code in debug mode with Qt Creator - c++

I am showing you my problem.
I'm creating an application in Qt Creator for a university project.
As you can see in the code below, I inserted two 'cout' inside a method just because they were useful to me to check that the program worked, this means that they have no real function.
After viewing it, the professor delegated me to do it in debug mode with #IFDEF so that that portion of the code containing the 'cout' is visible only to the programmer and not to a hypothetical client.
I hope I have explained my problem fairly well, I thank anyone who can help me.
Consider that in this type of thing I am quite novice.
Thanks

You have to use preprocessor macro. The correct one for qt creator is QT_DEBUG. Your code should look like this:
void BankAccount::addTransaction(Transaction* t){
if(t->getType()==0 && actualAccountBalance < t->getTransactionValue()) {
#ifdef QT_DEBUG
cout<<"Unable to add transaction: insufficient balance to make the requested withdrawal!"<<endl;
#endif
; // empty command, because of empty if
}
else if(find(transactions.begin(),transactions.end(),t) != transactions.end()) {
#ifdef QT_DEBUG
cout<<"This transaction has already been added!"<<endl;
#endif
; // empty command, because of empty if
}
else{
transactions.push_back(t);
assignID(t);
actualAccountBalance = calculateAccountBalance();
}
}

Related

Qt Creator does not autocomplete and colorize the code

I am having a problem with Qt. I am writing C++ code in Qt Creator but it doesn't autocomplete and colorize some of my functions.
Here is the sample of my code:
void Form::hide_button(){
this->ui->pushButton_10->hide();
}
void Form::on_pushButton_clicked()
{
if(push_button_error() == 0){
if(this->ui->comboBox_6->currentText() == "Evet"){
open_garph_dialog();
}
else{
}
}
}
For example, it autocompleted on_pushButton_clicked() function but it did not complete and colorize other functions. Both functions are defined in the header file.
Do you have any idea why it happened?
Various old Qt Creator versions have this problem. You can fix it with following steps.
go to Help
select About_Plugins
change ClangCodeModel (set or reset)
check
Some times the code update in the editor will be stuck, for that you can do following steps to fix it.
go to Tools
select About_Plugins
click Reset_Code_Model

QT Exception treatment

I've been trying to make a list in Qt that adds and removes integers for my Data Structures class, the teacher asked for a GUI, but I've been struggling a bit with the exception. I have managed to show the error when the list is empty, but the program closes after that, I wanted it to keep running after the warning popup window, but i have no idea how to proceed. I'm using try and catch to do that. Here is one of my remove buttons, this one removes the first int of the list.
void MainWindow::on_removerInicio_clicked()
{
try
{
throw ListaSeq.isEmpty();
}
catch(...)
{
QMessageBox::warning(this,tr("Aviso!"),tr("Lista Vazia!"));
}
ui->listWidget->takeItem(0);
ListaSeq.removeFirst();
nElementos--;
ui->nElementos->setText(QString::number(nElementos));
}
I know it's wrong but I don't know how to fix it, when clicked it shows the warning, but also closes the program. Can you help me?
Edit: Also if i take out the code
ui->listWidget->takeItem(0);
ListaSeq.removeFirst();
nElementos--;
ui->nElementos->setText(QString::number(nElementos));
The programs works fine and return to the mainwindow.

QMessageBox Compatibility

I'm looking through some Qt code and see that rather than just using QMessageBox, the program checks whether QAPPLICATION_H is defined. If it isn't, then it uses some default system message box. Here's what it looks like:
bool Connect()
{
...
{
#ifdef QAPPLICATION_H
QMessageBox::critical(0,QString("Error!"),QString("Cannot Connect To PS3"));
#else
MessageBoxA(0,"Error!","Cannot Connect To PS3",MB_ICONINFORMATION);
#endif
return false;
}
else
{
...
#ifdef QAPPLICATION_H
QMessageBox::information(0,QString("Sucess!"),QString("Connected To PS3!"));
#else
MessageBoxA(0,"Sucess!", "Connected To PS3", MB_ICONINFORMATION);
#endif
return true;
}
}
Basically, my question is: what's the compatibility of QMessageBox? If I released a program that only uses QMessageBox, will people without Qt not be able to see the message pop up? I just don't want to have to check for this every time in my own code, and also the standard non-Qt box looks worse.
Qt is cross platform QMessageBox will be available on any platform you compile your code. I don't know why in the listed code is that define and the call to MessageBoxA, maybe the developer wanted to be able to display a more windows look and feel message box, in case the target platform is windows.

AntTweakBar callbacks throwing " ';' expected" error before method

After following a tutorial to create a basic particle system, I began implementing AntTweakBar to get a basic UI going. This was going well, everything draws OK. But once I started adding the callback functions it started to go a little haywire, claiming that it's throwing this in the middle of a method definition:
Intellisense: expected a ';'
The method in question is:
void GLFWCALL OnMouseRight(int glfwButton, int glfwAction)
{
if( !TwEventMouseButtonGLFW(glfwButton, glfwAction) ) // Send event to AntTweakBar
{
if(glfwButton == GLFW_MOUSE_BUTTON_RIGHT)
computeMatricesFromInputs();
else
// Event has not been handled by AntTweakBar
// Do something if needed.
}
}
The error is apparently popping up between the name OnMouseRight and the following parentheses. How do I get rid of this error? I'm coding in OpenGL (using GLFW and GLEW) with C++ and I have been following the steps on this page on the AntTweakBar site. I have looked up most of the GLFW documentation already and not found much. Any help appreciated.
There is a missing statement after else which is causing the error:
if(glfwButton == GLFW_MOUSE_BUTTON_RIGHT)
computeMatricesFromInputs();
else
// ^ no statement here
You can add a semicolon as Intellisense suggests or an empty compound statement {}:
if(glfwButton == GLFW_MOUSE_BUTTON_RIGHT) {
computeMatricesFromInputs();
} else {
// Event has not been handled by AntTweakBar
// Do something if needed.
}
Right, after a long night's work at this problem and trawling through GLFW's documentation again I noticed that the problem lay with the macro GLFWCALL. C++ was treating it as a method in it's own right and as a result ignoring the actual method. GLFW's documentation explained why:
The GLFWCALL macro, which made callback functions use __stdcall on Windows, has been removed. GLFW is written in C, not Pascal. Removing this macro means there's one less thing for users of GLFW to remember, i.e. the requirement to mark all callback functions with GLFWCALL. It also simplifies the creation of DLLs and DLL link libraries, as there's no need to explicitly disable #n entry point suffixes.
On removing GLFWCALL and following the advice of #vitaut my program now handles the right mouse inputs correctly.

Visual Studio not building correctly

I've been having two different problems when trying to compile a C++ project today.
Sometimes it won't reflect any of my changes in the new build: If I change some wording on the output or change around actual functionality and compile and hit Start Debugging, it will behave exactly as it did before I made the changes. Hitting Clean or Rebuild Solution fixes this, but it takes about a full minute to compile this. I guess it's not detecting any changes, but in the output window I see it list the file names of files I made changes to...
I'm also getting a lot of "...already defined in main.obj" errors (one for every function and variable) whenever I try to use a header file or define a function outside of a class. Renaming the functions lets it compile once, but then the second compile will bring up the errors again. It kinda works if I just define the class in a .cpp file, don't use a header file, and don't use any functions outside of the class.
The project is an open-source program I downloaded the other day to mess with (I'm building a bot to control it). I didn't have the first problem until today, but the second one's always been happening. All I've done is add a file (two if you count both Bot.cpp and Bot.h); Bot.cpp includes Bot.h and some files from the program, and the program's main.cpp includes Bot.cpp.
I'll post some code I guess, but I can't find anything wrong with what I'm doing. I'm wondering if there's something I need to do to the existing files? (there were VS solution files included with the project that I used to open it, since VS Express doesn't help you create projects from existing code.)
//Bot.h
#ifndef _Bot_h_
#define _Bot_h_ 1
#include <string>
class Bot{
private:
uint32 inputs = 0;
bool active = false;
public:
Bot(){};
~Bot(){};
void Start();
void Stop();
void Run();
void Wait(int seconds);
void Press(int buttons);
};
#endif
//Bot.cpp
#ifndef _Bot_cpp_
#define _Bot_cpp_ 1
#include "main.h"
//Some other project files included between these
#include "Bot.h"
using namespace std;
void Bot::Start(){
if (active == false){
active = true;
Run();
}
}
void Bot::Stop(){
active = false;
}
void Bot::Run(){
while (active == true){
printf("Has my code updated?\n");
Wait(2);
}
}
//There are more functions defined here
#endif
All I've really done in the original source code is include Bot.cpp at the bottom of the list of includes in the main.cpp file, and then add a call to create the class. I'm a little out of practice with C/C++ so maybe it's something simple. I'm also bad at writing posts like this so if anything needs clarified I'll try to help.