'search' was not declared in this scope - c++

I am trying to learn C++, and I am using the Qt Framework. What I am currently trying to accomplish, is loading a custom font from the resource file. When I run this, File Found is output to the console window. When I uncomment the commented out line I get an error 'search' was not declared in this scope. In the Qt Creator I have objectName set to search. I assumed that I could then access it like how I wrote the commented out line, but I guess I can't. What am I doing wrong?
int main(int argc, char *argv[]){
QApplication a(argc, argv);
Apollo w;
w.show();
int fontID(-1);
QFile res(":/lib/fonts/SourceCodePro-ExtraLight.ttf");
if(res.open(QIODevice::ReadOnly) == true){
fontID = QFontDatabase::addApplicationFontFromData(res.readAll());
if(fontID == -1){
qDebug() << "File Not Found";
}else{
qDebug() << "File Found";
}
}
//search.setFont(QFont("Source Code Pro ExtraLight", 26));
return a.exec();
}

Your object names is "search".
The object is a part of the GUI I guess, so if you want to have acces to it you should do something like that:
ui->search->setFont(...);
Qt creator is quite smart and offer you a kind of auto complement. If it doesn't offer you a proposition for a object of the GUI most of the time that mean you do it wrong.

Related

How to create QT Login Page bedore Mainwindow?

My Qt windows application is ready, but when the application opens, I want the login dialog to be opened, how can I do this? I'm new to Qt and C++. It would be great if it was descriptive.
You have many ways to achieve that... QDialog is a nice way. Here is a short sample using QInputDialog.
One solution could be to add this code in your main.cpp file, and to load the mainwindow only if the credentials are ok.
#include "gmainwindow.h"
#include <QApplication>
#include <QInputDialog>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
GMainWindow w;
QString login = QInputDialog::getText(NULL, "Login","Name ?",QLineEdit::Normal);
if (login == "USER")
{
w.show();
}
else
{
//display an error message
return a.quit();
}
return a.exec();
}
Of course you may want to put an encrypted password and other things, but the idea will be more or less the same.

In qt QFileDialog setsuffix is not working in linux, how to solve?

I am working on a Save dialog for my qt app. Everything works, but if no file extension is added behind the filename, it won't automatically be saved with the file extension although the filter is selected.
I know i need to set a defaultsuffix option, but even if i do, then it still won't add the extension automatically if its not given.
I found several other similar questions, where i read it works in windows but it could fail on linux distro's. If so, is there a simple workaround? Because right now, i don't have a working solution...
void MainWindow::on_actionSave_Chart_As_triggered()
{
QFileDialog *fileDialog = new QFileDialog;
fileDialog->setDefaultSuffix("files (*);;AstroQt aqt (*.aqt)");
QString fileName = fileDialog->getSaveFileName(this, "Save Radix", ui->label_2->text() +".aqt", "AstroQT(*.aqt)");
qDebug() << " save file name " << fileName << endl;
QFile file(fileName);
if (!file.open(QFile::WriteOnly | QFile::Text)) {
QMessageBox::warning(this, "Warning", "Cannot save file: " + file.errorString());
return;
}
setWindowTitle(fileName);
QTextStream out(&file);
QString text = "text that will be saved...";
out << text;
file.close();
}
Edit: After trying multiple solutions, none seemed to work. But it should have, i guess. Why else is there a aftersuffix function...? For now i solved it doing it manually. But i'm not happy with it, there should be a better solution/explanation.
// add extension if none is found.
if(!fileName.endsWith(".aqt"))
fileName.append(".aqt");
If you use the static method getSaveFileName things seems to work correctly:
#include <QFileDialog>
#include <QApplication>
#include <QDebug>
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QString fileName = QFileDialog::getSaveFileName(
nullptr, QObject::tr("Save File"),
"teste.aqt",
QObject::tr("AstroQt (*.aqt)"));
qDebug() << " save file name " << fileName << endl;
return app.exec();
}
I get the correct file name with the extension, if I type something without the extension.
If you take a look at QFileDialog documentation, you will see that getSaveFileName() is an static function. Because of this, there is no way for this method to access a member of the instance of the class that makes use of setDefaultSuffix(). So whatever you set in fileDialog->setDefaultSuffix(...) has nothing to do with what the getSaveFileName() function does.
In ordertTo make it work, you have to run the dialog directly from the instance. You should do something like this:
QFileDialog fileDialog(this, "Choose file to save");
fileDialog.setDefaultSuffix("json");
fileDialog.setNameFilter("json-files (*.json)");
fileDialog.exec();
QFile f(fileDialog.selectedFiles().first());
QFileInfo fileInfo(f);
QString FILE_NAME(fileInfo.fileName());

vc++ + QT translation of UI is not working

I have a project with QT in vc++ and I need to locate the string in the UI to different languages. I created a UI through the QTdesigner in the visual studio add in of visual studio 2012 and also I have installed the QT plugin to use the Qt features as well.
I have created a .pro file and added:
SOURCES += main.cpp
TRANSLATIONS += languagefileqt_es.ts
After I generate a linguist file SOURCES emminensmultiportqt_es.ts and it detected correctly all the strings in my IU. After that, I generate the .qm file using the release function of Qtlinguist.
My resources file is:
<RCC>
<qresource prefix="MyAppQT">
<file>languagefileqt_es.qm</file>
</qresource>
</RCC>
Then I have added this to my main.cpp:
QTranslator translator;
bool loaded = translator.load("languagefileqt_es");
qDebug() << "loaded " << loaded;
a.installTranslator(&translator);
And loaded returns true in all the cases. My problem is that the UI is not translated when the application is executed. It is weird because it has no effect.
Any clue about what I am missing or what could I check out?
Thanks a lot
Are you sure, that "languagefileqt_es" is the correct name of your language file? I would expect "languagefileqt_es.qm" instead.
Are you sure that you are loading from the correct directory? Unless you are loading from an internal compiled-in resource (:/languagefileqt_es.qm) you should refer to an absolute path to make sure, that you load the correct thing.
I discovered what was the problem. thanks #Jens for try to help.
I think I commit a mistake of not knowing how the translating mechanism was working. In my main.cpp I had:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindowQT w;
QTranslator translator;
bool loaded = translator.load("languagefileqt_es");
qDebug() << "loaded " << loaded;
a.installTranslator(&translator);
w.show();
return a.exec();
}
But I realized that if I execute
qDebug() << QApplication::translate("MainWindowQTClass", "...BOARDING", 0);
after the loading process it will return the string translated correctly. So I change the definition of my UI after the internationalization and it worked. Apparently, translation is done in a function called retranslateUi() which is called in the constructor.
The correct main.cpp should be:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QTranslator translator;
bool loaded = translator.load("languagefileqt_es");
qDebug() << "loaded " << loaded;
a.installTranslator(&translator);
MainWindowQT w;
w.show();
return a.exec();
}

Qt - confused on QDialog choice on main

I have the following Qt code:
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
ChoosingDialog cdlg;
if(!startWin.exec())
{
// nothing chosen
return 0;
}
if(cdlg.firstWindowSelected)
{
CFirstWindow win;
win.show();
return app.exec();
}
else
{
CSecondWindow win;
win.show();
return app.exec();
}
}
this seems to work but it's giving me an error on "event dispatcher cleanup" in some asm line. I tried to trick a bit and I saw that the problem is related to the app.exec() calling.
Update:
if I add these lines to the ChoosingDialog (this is a simple blank class auto-generated by Qt Creator)
void ChoosingDialog ::closeEvent(QCloseEvent *)
{
exit(1);
}
I receive no errors
Turns out I was using the "singleapplication" class before the QApplication.. and something went wrong on the shared memory lock.
As soon as I restarted my system the exception disappeared... bof.. do you believe in magic?

Newbie problem with QT C++ - Qimage dont work?

I am trying to do console application to read pixels from image:
#include <QtCore/QCoreApplication>
#include <QtGui/QImage>
#include <iostream>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QImage *img = new QImage("adadad.jpg");
//std::cout << "Type filename:" << std::endl;
img->isNull();
return a.exec();
}
That doesn't work I got: (IT doesn't compile, but anyway file isn't exist yet...)
File not found: tmp/obj/debug_shared/main.o:: In function `main':
What is going on? Is it impossible to use Qimage with console app?!
EDIT:
screen
It is possible to use QImage in a console application, you must make sure that QtGui is configured though. If you chose a console app, your .pro file might contain something like
CONFIG += console
QT -= gui
If that's the case, remove the QT -= gui line.
QImage("adadad.jpg");
Will probably look for a file called adadad.jpg on the current working directory for your application. Check if that file is present. Otherwise, use a fully qualified path.
img->isNull() doesn't do anything on it's own, try this instead:
if(img->isNull())
std::cout << "Image isNull!\n";
else
std::cout << "Image loaded\n";
My guess is that the local directory of the executable is not the same as the location of that image, so Qt can't find the file. Try specifying the complete path.
EDIT: Ahh... didn't realize it was a compilation problem. That looks suspiciously like a moc issue. What build system are you using? and can you confirm that the moc step is executing?
This modification of your code will compile and run as expected if there is a valid image file in the current working directory when you run the app. It will display Image loaded
#include <QtGui/QImage>
#include <iostream>
int main(int argc, char *argv[])
{
QImage *img = new QImage("adadad.jpg");
if(img->isNull())
std::cout << "Image is null";
else
std::cout << "Image loaded";
return 0;
}
You do not need to create an instance of QCoreApplication unless you have subclassed it and put your program code in that subclass.
Update:
Your program does not exit so you are probably getting that compile error because it can't replace the executable because it is still running (and locked). The file locking is more likely to be an issue under Windows.
An important note when you are loading a file using directly "adadad.jpg" in your code. Even if you put the file inside the debug/release folder, QImage will always be null if loaded this way.
I run into this problem yesterday and I fixed it by using the Qt library to get the full path: QCoreApplication::applicationDirPath().
There is two way to achieve that, first one is when you create the img object.
QImage img( QCoreApplication::applicationDirPath() + "adadad.jpg");
if( img.isNull())
{
qDebug() << "Loading Error - file: adadad.jpg.";
return false;
}
or using the load function
QImage img;
if( !img.load(QCoreApplication::applicationDirPath() + "adadad.jpg"))
{
qDebug() << "Loading Error - file: adadad.jpg.";
return false;
}