Welcome! Log In Create A New Profile

Advanced

A1Q1 copy paste does not compile.

Posted by hideinlight 
Announcements Last Post
Announcement SoC Curricula 09/30/2017 01:08PM
Announcement Demarcation or scoping of examinations and assessment 02/13/2017 07:59AM
Announcement School of Computing Short Learning Programmes 11/24/2014 08:37AM
Announcement Unisa contact information 07/28/2011 01:28PM
A1Q1 copy paste does not compile.
January 28, 2011 03:41AM
To my understanding the code should work without changing anything, yet mine don't compile because of line 17:
connect(button, SIGNAL(clicked()), &app, SLOT(aboutQt()));

Now I can get it compile by changing it to:
QObject::connect(button, SIGNAL(clicked()), &app, SLOT(aboutQt()));

My question is why?

PS.
I'm on version 4.5.2 of QtCreator
Anonymous User
Re: A1Q1 copy paste does not compile.
January 28, 2011 11:37AM
Can you post the code? (I'm not registered for this module)
Re: A1Q1 copy paste does not compile.
January 31, 2011 05:14PM
Line 1 #include <QApplication>
Line 2 #include <QWidget>
Line 3 #include <QPixmap>
Line 4 #include <QLabel>
Line 5 #include <QPushButton>
Line 6 #include <QVBoxLayout>

Line 7 int main(int argc, char *argv[]) {
Line 8 QApplication app(argc, argv);
Line 9 QWidget window;
Line 10 QPixmap pixmap("../picture.jpg"winking smiley;
Line 11 QLabel * label = new QLabel;
Line 12 label->setPixmap(pixmap);
Line 13 QPushButton * button = new QPushButton("About Qt"winking smiley;
Line 14 QVBoxLayout * layout = new QVBoxLayout;
Line 15 layout->addWidget(label);
Line 16 layout->addWidget(button);
Line 17 connect(button, SIGNAL(clicked()), &app, SLOT(aboutQt()));
Line 18 window.setLayout(layout);
Line 19 window.show();
Line 20 return app.exec();
Line 21 }
avatar Re: A1Q1 copy paste does not compile.
February 01, 2011 07:59AM
connect is a static member of QObject. You cannot use it globally.

Try changing line 17 to:
QObject::connect(button, SIGNAL(clicked()), &app, SLOT(aboutQt()));
Re: A1Q1 copy paste does not compile.
February 01, 2011 02:29PM
Thanks, I had the same problem. Fixed now.

But I still can't get the picture to render... it only shows the button. Hmmm, I wonder if these assignments were properly proofed before being sent out...
avatar Re: A1Q1 copy paste does not compile.
February 02, 2011 04:55PM
@lost.pebble

Make sure that your picture is in the same project file / directory as the main.cpp
and that it is named the same as in the code (picture.jpg).
Re: A1Q1 copy paste does not compile.
February 02, 2011 08:24PM
no it wasn't that. The picture was in the folder with the main.cpp

but I worked it out now, I had to put the picture in the /question1-build-desktop folder for some reason... which seems to only be created when you build the program. It makes no sense to me why it would look there and not in the project folder. confused smiley

I'm using Qt Creator, as they stated we should, perhaps it works different from the command line..
Re: A1Q1 copy paste does not compile.
February 02, 2011 08:51PM
I did not even put it into my project folder and yet it displayed it fine. I just placed the picture in my c:\. I was also using the Qt Creator
avatar Re: A1Q1 copy paste does not compile.
February 03, 2011 10:21AM
Quote
lost.pebble
but I worked it out now, I had to put the picture in the /question1-build-desktop folder for some reason... which seems to only be created when you build the program. It makes no sense to me why it would look there and not in the project folder.

If you're using QtCreator, and you go to the Projects window and look at the Builds tab, you'll see that there's an option to allow a "Shadow build". What this is for is to allow you to separate your compiler output files from your source files. If you select the option, you'll see that it will change the Build Directory to <project directory>-build-desktop, as you said above. This will only be created at build time if it doesn't exist. If you don't want your build files and code files to be separated, deselect the Shadow Build option.

Since your executable will be in the build directory, you need to set the correct path in your source files to where the picture is found.

Assuming you have the following directory structure:

projects
   + --- a1q1
   |       + --- main.cpp
   |       + --- picture.jpg
   |
   + --- a1q1-build-desktop
           + --- main.o
           + --- a1q1.exe

If you switch to the "Run Settings" tab, have a look at the "Working Directory" settings. This is where all files should be referenced from if you use relative file paths (as you did when loading the picture file name). You should either copy your picture file into the working directory and set the reference to "picture.jpg" or you should leave it where it is and change the directory reference it back to the directory where your source files are. In the above file tree, this would be "../a1q1/picture.jpg"
Re: A1Q1 copy paste does not compile.
February 03, 2011 11:14AM
ahhh.... thank you so much robanaurochs, that cleared up the confusion. It all boils down to the working directory, it seems strange that they don't make the project folder the default directory. But oh well, at least I've learned something useful in the process. much appreciated
Re: A1Q1 copy paste does not compile.
February 19, 2011 05:13PM
Thank goodness for other students! What would we do without you guys helping out!
avatar Re: A1Q1 copy paste does not compile.
February 21, 2011 08:31AM
Yes. Thanks robanaurochs.

_____________________________________
The sun is always shining, but it is far away.
avatar Re: A1Q1 copy paste does not compile.
February 21, 2011 09:35AM
@Hideinlight

I forgot to mention that you can make the forum format your code so you don't lose the indentation and that it also doesn't misinterpret your parentheses as emoticons. To do this, just paste your code into the editor box and then select the text that you want specially formatted.

Just above the text box that you type in is a tool bar with buttons for formatting your text. From left to right it goes, bold, italics, underline, strike-through, etc. The one for code formatting is towards the right, between Quote and Left Justify. It looks like a small square.

Just select the programming code that you want formatted, click on the formatting button. You can optionally choose a language if you want special syntax colours. You can also select (down the bottom) whether you want line numbers added or not.

If I use the C++ option and apply it to the following:

#include <iostream>
using namespace std;

int main(int argc, char **argv){
cout << "Hello world" << endl;
return 0;
}

I get the following:

Language: C++
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main(int argc, char **argv){
  5. cout << "Hello world" << endl;
  6. return 0;
  7. }


Hope you read this so you don't have to type as much (ie line numbers) next time you past code.
Re: A1Q1 copy paste does not compile.
February 24, 2011 02:26AM
Thx robanaurochs, that will come in handy. Was wondering about the absence of the code box (use to another icon from other forums).
Sorry, only registered users may post in this forum.

Click here to login