User
Pass
2FA
 
 

[C++] Cum adaug o pictograma?

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Freakz Forum Index -> Trash Bin -> Trash -> Programming / Scripting / Database
Author Message1749
4drian

[Mentally Stable]



Status: Offline
(since 06-11-2009 17:32)
Joined: 07 Sep 2009
Posts: 10, Topics: 6
Location: Romania

Reputation: 91.7
Votes: 4

Post Posted: 07-09-2009, 16:57:13 | Translate post to: ... (Click for more languages)

Sistemul de operare pe care il folosesc este Linux Ubuntu, iar toolkit-ul este Qt.Vreau sa implementez in urmatorul program o pictograma -

Fisierul main.cpp

Code:
#include "HelloWidget.h"
#include <QDesktopWidget>
#include <QApplication>

// function used for setting the widget position and maximum/minimum size
void setWidgetPosition(QWidget* widget)
{
    // get the desktop
    QDesktopWidget *desktop = QApplication::desktop();
    // move the widget to the center of the screen
    widget->move((desktop->width() - widget->sizeHint().width()) / 2,
                 (desktop->height() - widget->sizeHint().height()) / 2);

    // set the minimum and maximum size of the widget
    // it won't be possible to resize the widget
    // above the maximum or below the minimum limits
    widget->setMaximumSize(2 * widget->sizeHint().width(), widget->sizeHint().height());
    widget->setMinimumSize(widget->sizeHint());
}

int main(int argc, char *argv[])
{
  // this is our application
  QApplication app(argc, argv);

  // we create a HelloWidget object
  HelloWidget hello;

  // set the name displayed in the title bar for our object
  hello.setWindowTitle(QObject::tr("Utilizator"));
  // set the position of our widget
  setWidgetPosition(&hello);
  // make it visible
  hello.show();

  // start the event loop
  return app.exec();


Fisierul HelloWidget.cpp

Code:
#include <QLineEdit>
#include <QLabel>
#include <QPushButton>
#include <QVBoxLayout>
#include <QMessageBox>
#include <QRegExp>
#include <QRegExpValidator>
#include <QKeyEvent>
#include <QApplication>

#include "HelloWidget.h"

const int MAX_NAME_LENGTH = 256;

HelloWidget::HelloWidget(QWidget* parent) :
        QWidget(parent)
{
    actionSubMenu1 = new QAction(this);
    actionSubMenu1->setObjectName("actionSubMenu1");
    actionSubMenu1->setText("SubMenu1");
    actionSubMenu2 = new QAction(this);
    actionSubMenu2->setObjectName("actionSubMenu2");
    actionSubMenu2->setText("SubMenu2");

    menuBar = new QMenuBar(this);
    menuBar->setObjectName("menuBar");
    menuBar->setGeometry(QRect(0, 0, 200, 40));
    menuMenu1 = new QMenu(menuBar);
    menuMenu1->setObjectName("menuMenu1");
    menuBar->addAction(menuMenu1->menuAction());
    menuMenu1->addAction(actionSubMenu1);
    menuMenu1->addSeparator();
    menuMenu1->addAction(actionSubMenu2);
    menuMenu1->setTitle("Menu1");
    connect(actionSubMenu1, SIGNAL(triggered()), this, SLOT(OnSubMenu1()));

    // create a horizontal layout which will contain a label and the m_nameLineEdit
    // control, used for entering the name
    QHBoxLayout* hbox1 = new QHBoxLayout();

    // create a label and add it to hbox1 layout
    QLabel* label = new QLabel(tr("Introduceti numele: "), this);
    hbox1->addWidget(label);

    // create the m_nameLineEdit control and attach a regexp validator to it
    // the regext validator will check that string introduced in the control
    // represents a valid name
    QRegExp regex("((\\s*)([a-zA-Z]+)(\\.*)(\\s*))+");
    QRegExpValidator* regexValidator = new QRegExpValidator(regex, this);
    m_nameLineEdit = new QLineEdit(this);
    m_nameLineEdit->setValidator(regexValidator);

    // connect the slots to the signals, for the m_nameLineEdit control
    connect(m_nameLineEdit, SIGNAL(textChanged(QString)), this, SLOT(onTextChanged()));
    connect(m_nameLineEdit, SIGNAL(returnPressed()), this, SLOT(onEnterPressed()));

    // add the control tp the hbox1 layout
    hbox1->addWidget(m_nameLineEdit);

    // create a new horizontal layout, which will contain the OK and Clear buttons
    QHBoxLayout* hbox2 = new QHBoxLayout();

    // create the OK buttom, set as disabled initially
    m_okButton = new QPushButton(tr("OK"), this);
    m_okButton->setEnabled(false);

    // connect the signals to the slots for the OK button
    connect(m_okButton, SIGNAL(clicked()), this, SLOT(onOkButtonClicked()));

    // add the control to the hbox2 layout
    hbox2->addWidget(m_okButton);

    // create the Clear button, set as disabled initially
    m_clearButton = new QPushButton(tr("Clear"), this);
    m_clearButton->setEnabled(false);

    // connect the signals to the slots for the Clear button
    connect(m_clearButton, SIGNAL(clicked()), this, SLOT(onClearButtonClicked()));

    // add the control to the hbox2 layout
    hbox2->addWidget(m_clearButton);

    // create a vertical layout that will contain the first two horizontal layouts with
    // a vertical stretch btween them
    QVBoxLayout* vbox = new QVBoxLayout(this);
    vbox->addLayout(hbox1);
    vbox->addStretch();
    vbox->addLayout(hbox2);

    // set the vertical layout as the layout for our HelloWidget object
    setLayout(vbox);
}

// this is called when the text changes in the m_lineEdit control
// if we have no text available in the control, disable the OK and Clear buttons
void HelloWidget::onTextChanged()
{
    if (m_nameLineEdit->text() > 0)
    {
        m_okButton->setEnabled(true);
        m_clearButton->setEnabled(true);
    }
    else
    {
        m_okButton->setEnabled(false);
        m_clearButton->setEnabled(false);
    }
}

// this is called when the user press the ENTER key with the focus on m_nameLineEdit
// the name entered by the user in the m_nameLineEdit will be shown in a message box
void HelloWidget::onEnterPressed()
{
    showName();
}

// this is called when the OK button is clicked
// the name entered by the user in the m_nameLineEdit will be shown in a message box
void HelloWidget::onOkButtonClicked()
{
    showName();
}

// this is called when the clear button is clicked
// the focus is set on the m_nameLineEdit, which gets cleared
void HelloWidget::onClearButtonClicked()
{
    m_nameLineEdit->clear();
    m_nameLineEdit->setFocus();
}

// this is called when a key is pressed
// if the key got pressed is ESC, we quit the application
// if we pressed return, we check if the focus is on the m_okButton or m_clearButton
// if it is, we execute their clicked() slots
void HelloWidget::keyPressEvent(QKeyEvent * event)
{
    if (event->key() == Qt::Key_Escape)
        qApp->quit();
    else if(event->key() == Qt::Key_Return)
    {
        if (m_okButton->hasFocus())
            onOkButtonClicked();
        else if (m_clearButton->hasFocus())
            onClearButtonClicked();
    }
}

// this is called to show the name entered in the m_nameLineEdit control, in a message box
// we check if the name is no more than MAX_NAME_LENGTH characters (otherwise
// we display and error)
// after displaying the message, we clear the m_nameLineEdit control and set focus on it
void HelloWidget::showName()
{
    QString userName = m_nameLineEdit->text();
    if (userName.size() > MAX_NAME_LENGTH)
    {
        showNameToLongError();
        return;
    }
    QMessageBox::information(this, tr("Informatii utilizator"),
                             tr("Numele introdus este: ") + userName);
    m_nameLineEdit->clear();
    m_nameLineEdit->setFocus();
}

// function used to show and error, in case the name entered in the m_nameLineEdit
// control is bigger than MAX_NAME_LENGTH characters
// after displaying the message, we clear the m_nameLineEdit control and set focus on it
void HelloWidget::showNameToLongError()
{
    QMessageBox::critical(this, tr("Eroare"),
                             tr("Numele introdus este prea lung !"));
    m_nameLineEdit->clear();
    m_nameLineEdit->setFocus();
}

void HelloWidget::OnSubMenu1()
{
    QMessageBox::information(this, "Mesaj",":-)");
}


Fisierul HelloWidget.h

Code:

#ifndef HELLOWIDGET_H
#define HELLOWIDGET_H

#include <QWidget>

#include </QAction>
#include </QMenu>
#include </QMenuBar>

// forward class declarations
class QLineEdit;
class QPushButton;
class QKeyEvent;

class HelloWidget : public QWidget
{
    Q_OBJECT

    public:
        QAction *actionSubMenu1;
        QAction *actionSubMenu2;
        QMenuBar *menuBar;
        QMenu *menuMenu1;

         /**
         *@brief constructor for HelloWidget class
         *@param parent the parent of this widget
         */
        HelloWidget(QWidget *parent = 0);

    private slots:
        void OnSubMenu1();

        /**
         *@brief slot called when m_nameLineEdit emits the textChanged() signal
         */
        void onTextChanged();

        /**
         *@brief slot called when ENTER is pressed with focus on m_nameLineEdit control
         */
        void onEnterPressed();

        /**
         *@brief slot called when the m_okButton button is clicked
         */
        void onOkButtonClicked();

        /**
         *@brief slot called when the m_clearButton button is clicked
         */
        void onClearButtonClicked();

    private:
        /**
         *@brief function called when a key is pressed
         *@param event the key event that generated the signal
         */
        void keyPressEvent(QKeyEvent* event);

        /**
         *@brief shows the name of the user in a message box
         */
        void showName();

        /**
         *@brief shows an error message if the user entered a name larger than 256 characters
         */
        void showNameToLongError();

        /**
         * the line edit control where the user enters the name
         */
        QLineEdit* m_nameLineEdit;
        /**
         * the OK button
         */
        QPushButton* m_okButton;
        /**
         * the clear button
         */
        QPushButton* m_clearButton;
};

#endif


Ma ajuta cineva? Multumesc anticipat.

0 0
  
Back to top
View user's profile Send private message
4drian

[Mentally Stable]



Status: Offline
(since 06-11-2009 17:32)
Joined: 07 Sep 2009
Posts: 10, Topics: 6
Location: Romania

Reputation: 91.7
Votes: 4

Post Posted: 09-09-2009, 14:29:50 | Translate post to: ... (Click for more languages)

SO, can you help me?
0 0
  
Back to top
View user's profile Send private message
*0ranGe ! extrem

[I ❤ MY POLO!]



Status: Offline
(since 11-12-2017 12:06)
Joined: 01 Jul 2007
Posts: 11419, Topics: 191
Location: Romania

Reputation: 683.4
Votes: 117

   
Post Posted: 09-09-2009, 16:32:10 | Translate post to: ... (Click for more languages)

Asteapta sa iti raspunda Shocker, el nu e acasa momentan. Nu mai face dublu post.


0 0
  
Back to top
View user's profile Send private message
4drian

[Mentally Stable]



Status: Offline
(since 06-11-2009 17:32)
Joined: 07 Sep 2009
Posts: 10, Topics: 6
Location: Romania

Reputation: 91.7
Votes: 4

Post Posted: 09-09-2009, 18:08:06 | Translate post to: ... (Click for more languages)

Ok, imi cer scuze.
0 0
  
Back to top
View user's profile Send private message
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Freakz Forum Index -> Trash Bin -> Trash -> Programming / Scripting / Database  


The time now is 29-04-2024, 00:17:19
Copyright info

Based on phpBB ro/com
B

 
 
 







I forgot my password


This message appears only once, so
like us now until it's too late ! :D
x