Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8929611
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:42:41+00:00 2026-06-15T08:42:41+00:00

I’m trying to learn C++ using Qt for some basic visual applications. I want

  • 0

I’m trying to learn C++ using Qt for some basic visual applications. I want to make something simple at start, when I push a button, I want to display a text message somewhere. Here is my code:

main.h

#ifndef MYAPP_H
#define MYAPP_H

#include <QWidget>

class QLabel;
class QString;

class MyApp : public QWidget{
    public:
        MyApp(QWidget *parent = 0);
    protected slots:
        void showIt();
    private:
        QString *text_msg;
        QLabel *message;
};

#endif

main.cpp

#include <QApplication>
#include <QFont>
#include <QPushButton>
#include <QWidget>
#include <QLabel>
#include <QVBoxLayout>
#include <QFrame>
#include <QString>
#include <vector>
#include <string>
#include <map>
#include "main.h"

#include <fstream>

using std::map;
using std::vector;
using std::string;
using std::ofstream;



/* implementation */
MyApp::MyApp(QWidget *parent):QWidget(parent){

    QString text_msg;
    text_msg = "This is my first message written in C++! \n It was printed with Qt!";

    setFixedSize(400, 280);

    QPushButton *quit = new QPushButton(tr("Quit"), this);
    quit->setGeometry(62, 40, 75, 50);
    quit->setFont(QFont("Times", 18, QFont::Bold));

    QPushButton *show_msg = new QPushButton(tr("Show!"), this);
    show_msg->setGeometry(30,15,75,45);
    show_msg->setFont(QFont("Times", 18, QFont::Bold));


    //message = new QLabel();
    QLabel *message = new QLabel(this);
    //message->setFrameStyle(QFrame::Panel | QFrame::Sunken);
    //message->setText(text_msg);
    //message->setText("asdf");

    connect(quit, SIGNAL(clicked()), qApp, SLOT(quit()));
    connect(show_msg, SIGNAL(clicked()), qApp, SLOT(showIt()));

    QVBoxLayout *layout = new QVBoxLayout;


    layout->addWidget(message);
    layout->addWidget(show_msg);
    layout->addWidget(quit);


    setLayout(layout);

        ofstream myfile;
    myfile.open ("example");
    myfile << "Writing this to a file.\n";
    myfile.close();
}

void MyApp::showIt(){   
    //*text_msg = "xyz";
    ofstream myfile;
    myfile.open ("example");
    myfile << "12121212121.\n";
    myfile.close();
    message->setText("1234");
}



int main(int argc, char *argv[])
{
    /* assign messages for output 
    bool result;
    string key = "first", message="this is a sample text";
    result = Messages::add_message(key, message);
    */
    /* end */
    QApplication app(argc, argv);
    MyApp my_simple_app;
    my_simple_app.show();
    return app.exec();

}

I don’t understand why the program doesn’t run the slot member function. I put there a some code that should print in a file some text to know if the code inside that function will be executed and the problem is at the QLabel message, but the member function are not executed.

Anyone can help me?

Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T08:42:42+00:00Added an answer on June 15, 2026 at 8:42 am

    There are 3 things that I needed to change to your code to make it work:

    Firstly, in main.h you need to use the Q_OBJECT macro:

    class MyApp : public QWidget {
       Q_OBJECT
    

    Secondly, in main.cxx, you need to change the connect call to the correct receiver (this instead of myApp):

    connect(show_msg, SIGNAL(clicked()), this, SLOT(showIt()));
    

    Thirdly, in main.cxx, you need to uncomment the code that creates the message label as a class member:

    message = new QLabel(this);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm making a simple page using Google Maps API 3. My first. One marker
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.