Qt Error: ‘string’ was not declared in this scope.

Oddly enough, I ran into this same problem while using SFML. Here in Qt-creator, I’m getting this error while trying to use a string. I declared something like so:

#include <QCoreApplication>
#include <QDebug>
#include <QList>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QList<String> myStringList;

    myStringList << "Howdy."
                 << " I "
                 << " like this"
                 << " course on Qt.";

    myStringList.append(" I hope");
    myStringList.append(" you do too!");

    //qDebug() << myStringList[1];

    for (int i=0; i < myStringList.count(); i++) {
        qDebug() << myStringList[i];
    }

    return a.exec();
}

And I get the error that the string is not declared in this scope! So, I tried adding:

#include <string>

to add the string library from C++. However, then I got errors when returning those strings to qDebug, which said it couldn’t take a ‘char’ as a ‘string’.

Well, just like SFML, Qt has its very own string library, and it is automatically included, called QString, like this:

QList<QString> myStringList;

QString automagically changes the string to character or strings or whatever to make it work when you need it to. Seems like a bit of laziness for me as a programmer, but pretty handy and very convenient in my Qt apps!

Linux – keep it simple.

Advertisements

QLayout::addChildLayout: layout already has a parent!

qt_4_31

While working with layouts in Qt creator, I ran into an interesting problem. I am trying to add my “gridLayout_2” (the box with 9 push buttons) into the “myGrid” (the box with 8 text labels). When I run this code:

#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    QGridLayout * myGrid = new QGridLayout(this);


    myGrid->addWidget(ui->label,0,0);
    myGrid->addWidget(ui->label_2,0,1);
    myGrid->addWidget(ui->label_3,0,2);
    myGrid->addWidget(ui->label_4,1,0);
    myGrid->addWidget(ui->label_5,1,1);
    myGrid->addWidget(ui->label_6,1,2);
    myGrid->addWidget(ui->label_7,2,0);
    myGrid->addWidget(ui->label_8,2,1);
    myGrid->addLayout(ui->gridLayout_2,2,2);
    setLayout(myGrid);

}

Widget::~Widget()
{
    delete ui;
}

I get this error:

QLayout::addChildLayout: layout “gridLayout_2” already has a parent

And the “gridLayout_2” is not added to the “myGrid” layout. At first I thought I was doing something wrong (maybe I still am), but I found that this is a known bug in Qt Creator:

https://bugreports.qt.io/browse/QTBUG-24420

QBoxLayout::addLayout() and QGridLayout::addLayout() functions should reparent the added layout automatically if it already has a parent. Currently these functions call QLayout::addChildLayout() which does not add the child layout if it already has been added to some other layout i.e. has a parent.

Please note that QBoxLayout::addWidget() and QGridLayout::addWidget() reparent the added widget. Thus there is an inconsistency on the functionality.

So I didn’t figure out a way to add a grid layout inside one of the grids of another grid layout. If you figure this out, then be sure to let me know!

Linux – keep it simple.

Qt Creator, changing the default template for .pro files….

qt_change_default

The .pro file holds key information for your project that is used for compiling.

One issue that I have previously mentioned here was that certain styles of lambdas require you to use a C++ standard of 2011 or newer. This can be quickly overcome by adding the proper QMAKE flags to your .pro file in your project directory.

This becomes a bit tedious though, when you forget that and need to re-find the QMAKE flags so you can add them to your .pro file every time that you start a new project. Some web searching lead me to the conclusion that the templates for the files created in Qt Creator are held here:

/usr/share/qtcreator/templates/wizards

There are numerous folders here, each pertaining to a different kind of application/program that you can make. Within each folder are the template files used to create the default files you get when you create a new project. If you open them as a root user, you can edit them to your hearts content and then save them. Next time that you use Qt Creator to create a new project, these now edited files will be used (no restarting Qt Creator required).

This works great, and I have fixed numerous .pro files for console apps, etc., however, the one I can’t find is the Qt Widget Application .pro file, and that is the one I use the most! So I’ll keep looking. In the mean time, you can use these to edit your other default files.

Linux – keep it simple.

error: no matching function for call to ‘QAction::QAction(const char [5])’

qt_4_21

While working through section 4 of my Qt GUI creator course, I ran into an interesting error:

/home/alaskalinuxuser/Documents/qt_course/section_4_21_qmainwindow/mainwindow.cpp:19: error: no matching function for call to ‘QAction::QAction(const char [5])’
QAction * quitAction = new QAction(“Quit”);
^

What made the error interesting was that my code mirrored the instructors to a T:

// Now some QActions….
// Quiting
QAction * quitAction = new QAction(“Quit”);
// And connect it.
connect (quitAction,&QAction::triggered,[=](){
QApplication::quit();
});

However, after reading up a bit on it, I needed to add “this” to my QAction, like so:

// Now some QActions….
// Quiting
QAction * quitAction = new QAction(“Quit”, this);
// And connect it.
connect (quitAction,&QAction::triggered,[=](){
QApplication::quit();
});

Once I did that, it was smooth sailing. I don’t know for sure, it may be because the instructor was using Windows with MinGW and I am using Linux with GCC, or if it is a Qt version difference. Either way, if you run into this error, here’s the quick fix!

Linux – keep it simple.

Qt Creator warning: ‘auto’ changes meaning in C++11; please remove it

Just wrapped up section 3 of my Qt C++ beginner GUI course! Boy, this course is fun! I really appreciate the instructor taking the time to show 3 different ways to do things, and explaining why one might be more useful than others in different situations. I did run into a couple of errors though:

warning: ‘auto’ changes meaning in C++11; please remove it

And this one, too:

error: no matching function for call to ‘Widget::connect(QPushButton*&, void (QAbstractButton::*)(bool), Widget::Widget(QWidget*)::<lambda()>)’
});

Both of these are related, so I thought I’d bundle them together in this post. Apparently, GCC for Linux, while super smart, is so flexible that you can, of course, use different C++ standards, however, by default, it uses the OLDEST one. WHY?! So, in Qt creator, you have to tell it specifically to use the latest C++ standard, like so:

#-------------------------------------------------
#
# Project created by QtCreator 2018-06-22T09:40:37
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = section_3_18
TEMPLATE = app


SOURCES += main.cpp\
        widget.cpp

HEADERS  += widget.h

FORMS    += widget.ui

QMAKE_CXXFLAGS = -std=c++14
QMAKE_LFLAGS = -std=c++14

Those last two QMAKE lines are the ones I added. You can set them to any standard you like that is in your compiler, such as c++11, c++14, c++1z (well, on mine that is the 2017 standard, some newer ones may have c++17). Adding those two lines and saving the file will fix it.

The second error was caused because I had Qt4 and Qt5 installed. Obviously, you should use the newest one available to you. So, I had to click:

Tools->Options->Kits->Desktop

qt_3_17

And choose the latest Qt5 version, rather than the default of the lower Qt4 version. It took me a while of web browsing to figure this out, so hopefully this will save someone else some time!

Linux – keep it simple.

Qt 5 and C++ meet in a graphical user interface!

Section 2 is complete of the “Beginning Qt 5 C++ GUI Development” course! This course is great! I’m really learning a lot from the instructor. Section 2 was sort of a C++ primer, for those who were not familiar with it. As you know, if you’ve been following along here, I already learned C++ in another course. However, in this “primer” of C++, I gained a much deeper understanding of what exactly classes are, how to properly use constructors and all sorts of great stuff!

qt_gui

Check out my “Guess the number” game!

I’m not sure if I think the course is so great because I already know a little about C++, or if it just is that informative, but it seems to me that it moves at a great pace for beginners like myself.

You can check out my completed game, although it’s not much to see yet. The object is to guess the number which is between 1 and 10. You’ll have to import it into Qt-Creator and then compile it. I haven’t started making executables yet.

As an aside, if you are familiar with Android Studio, Qt-Creator is somewhat similar in feel and use. I like how you can right click on an object and add code directly to your cpp files, which is really handy!

Linux – keep it simple.

New Course! Beginning Qt 5 C++ GUI Development : The Fundamentals

I’ve just completed the first of 10 sections in a new course I am taking on Udemy. The course is called Beginning Qt 5 C++ GUI Development : The Fundamentals. As you know, if you’ve been following along, I started learning C++ using a course on Udemy that particularly focuses on graphics through SFML. It was a lot of fun learning to program in C++ while making a few games.

While making those games was really neat, I realized that I now need a way to make applications that aren’t games. I suppose you could use SFML for that, but it seems a bit overkill to make your own buttons for everything, when they have great programs for that already. Programs like Qt and Qt-Creator.

Licensed under the GPL-2 and GPL-3, Qt is open source, which is a big plus for me. Qt is also written in C++, which I am currently learning, so that is the primary reason that I want to use it. And did I mention that it’s cross platform? The IDE is Qt-Creator, which is licensed under the LGPL, a copyleft license, which makes it free for me to use and open sourced for those who wish to contribute, which is great!

qt_2

I’ve only just started working with it, but I really like the user interface of Qt-Creator thus far. The course itself has a huge plus for me, in that the author/instructor also supports Linux users, even providing a video tutorial for installing the application and how to deal with a few possible errors that may pop up if the right dependencies are not installed. That is really rare these days!

If you want to follow along, be sure to check out my first Qt commit!

Linux – keep it simple.