PDA

View Full Version : class Ui::MainWindow has no member named horizontalSlider



alexcodercpp
18th June 2015, 22:14
I was following a signal&slot tutorial I found out on youtube on how to create a progressBar and horizontalSlider just by coding, without drag&drop.

This is the code (mainwindow.cpp file):


#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->horizontalSlider,
SIGNAL(valueChanged(int)),
ui->progressBar,
SLOT(setValue(int)));
}

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



As you see, all the job was to add the text in the constructor.
It fails to compile on my laptop: "class Ui::MainWindow has no member named horizontalSlider and class Ui::MainWindow has no member named progressBar".
However on the video I was following it compiles and the author didn't make any additional writings.
I would highly appreciate your suggestions, guys.
I'm using Qt 5.4.1, maybe it's because of version inconsistencies and that tutorial is outdated?

jefftee
18th June 2015, 23:27
Open your MainWindow.ui file and look at the properties of each control on the form. Additionally, when you type ui-> and pause, you should get a code completion window which you can browse through and identify your horizontal slider control.

alexcodercpp
20th June 2015, 15:47
This is my mainwindow.ui file:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget"/>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>25</height>
</rect>
</property>
</widget>
<widget class="QToolBar" name="mainToolBar">
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>
</attribute>
<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
</widget>
<widget class="QStatusBar" name="statusBar"/>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>

Please, could you explain me what's going on here?

Regarding the completion window - here is what I see:
centralWidget
mainToolBar
menuBar
retranslateUi
setupUi
statusBar
MainWindow
Ui_MainWindow.

anda_skoa
20th June 2015, 17:16
Looks like you forgot to add both the slider and the progressbar while editing the form in designer.
That mainwindow has an empty content widget.

Cheers,
_

Radek
20th June 2015, 17:19
Open the file in the Designer not in a text editor. Yes, it's an XML file which describes the UI but it is not human-readable (almost :) ). Therefore
(1) If you have Designer installed, run the Designer, select "Open", select your ui file.
(2) Otherwise, open your project in the Creator, open "forms", select your ui.file.

You will see your UI, check attributes of MainWindow and see, whwther it has the scrollbar (I am not sure it can, You will need QScrollArea for it).

alexcodercpp
20th June 2015, 18:51
Looks like you forgot to add both the slider and the progressbar while editing the form in designer.
That mainwindow has an empty content widget.

Cheers,
_
anda_skoa, my task was to evoid drag&drop, doing everything just by coding only.

d_stranz
20th June 2015, 19:13
my task was to evoid drag&drop, doing everything just by coding only.

If you like doing things the hard way, then



delete your mainwindow.ui file and ensure it is removed from the project,
remove the #include "ui_mainwindow.h" line,
remove the "Ui::MainWindow * ui" variable from your MainWindow class header,
remove the "ui( new Ui::Mainwindow )" initialization from the constructor
remove the "ui->setupUi()" line
remove the "connect()" line
remove the "delete ui;" line from the destructor
run qmake, then build your project


Now you have an empty MainWindow class where you will be forced to add C++ code to create everything except those things already built into the QMainWindow class. Don't ask "Where's the horizontal slider", because there isn't any. There isn't any progress bar either. You will have a bare naked QMainWindow to which you will have to add (through code, not drag and drop) anything you want in it.

alexcodercpp
20th June 2015, 19:52
d_stranz, I've done everything you listed out.
Now the constructor looks this way:


MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
QProgressBar* progBar = new QProgressBar(this);
QSlider* slider = new QSlider(this);
}



Am I on the right path?

ChrisW67
20th June 2015, 23:05
Yes, but perhaps you should read some of the material in Qt Assistant before you continue
Address book tutorial
Layout Examples

You could get a hold of "C++ GUI Programming with Qt 4, 2nd Ed" by Blanchette and Summerfield. (Very little in Qt5 differs from the Qt4 examples. First Ed call be downloaded (http://www.qtrac.eu/marksummerfield.html) but differs more).

Something else you could do is build a *.ui file using Designer (outside your project), run it through uic and read the output. You can see how the Designer constructs the UI you designed (it just writes code for you).


C:\> designer test.ui
C:\> uic test.ui > test.cpp
C:\> notepad test.cpp

anda_skoa
21st June 2015, 10:16
anda_skoa, my task was to evoid drag&drop, doing everything just by coding only.

Well, you said you are doing a signal&slot tutorial and since you were using ui-> in your connect I assumed you were doing the UI in designer.

Not doing the signal/slot connection via designer did, for me, not imply abstaining from using the designer for creating the UI itself.

After all, doing the UI in designer and connecting signal/slots in code is the most widely used combination in actual projects.

Cheers,
_

alexcodercpp
21st June 2015, 14:06
Ok, I'm about to close this topic as solved, one question keeps me intrigued however: why in the tutorial I was following was no error ?
https://www.youtube.com/watch?v=cy5QqXZ5RCo (https://www.youtube.com/watch?v=cy5QqXZ5RCo) (voidrealms youtube channel qt playlist)
watch it from 2m18s: he deletes progressBar and horizontalSlider and starts writing the code. Maybe I misunderstood something?

anda_skoa
21st June 2015, 14:32
watch it from 2m18s: he deletes progressBar and horizontalSlider

No, he does not.
You can clearly see that both widgets are still on the dialog form when he switches back to the code editior.

Cheers,
_

alexcodercpp
21st June 2015, 16:04
Now I see. It was my lack of attention. However your answers were very informative and educational to me. Thanks to everybody.