Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. why the image appears at the corner of the window?
Qt 6.11 is out! See what's new in the release blog

why the image appears at the corner of the window?

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 256 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    user123
    wrote on last edited by
    #1

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),ui(new Ui::MainWindow),
    start(0, 0), end(0, 0), firstClick(true)

    {
    ui->setupUi(this);
    lbl=new Label(this);
    QPixmap pix("C:/Users/Utilisateur/Documents/paint/icecream.jpg");

        lbl->setPixmap(pix);
    

    }

    MainWindow::~MainWindow()
    {
    delete ui;

    }

    Label::Label(QWidget * parent)
    : QLabel(parent)

    {

    }
    void Label::mousePressEvent(QMouseEvent *pQEvent)
    {
    if (pQEvent->button() == Qt::LeftButton) {
    (firstClick ? start : end) = pQEvent->pos();
    firstClick = !firstClick;
    update();
    pQEvent->accept();
    }
    }
    void Label::paintEvent(QPaintEvent *pQEvent)
    {
    QLabel::paintEvent(pQEvent);
    if (!firstClick) return;
    QPainter painter(this);
    QPen pen(Qt::red);
    pen.setWidth(4);
    painter.setPen(pen);
    painter.drawLine(start, end);
    }

    void MainWindow::on_pushButton_clicked()
    {
    double distance=sqrt(pow(start.x()-end.x(), 2) + pow(start.y()-end.y(), 2));
    ui->label->setText(QString::number(distance));
    }

    .h
    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    QLabel *lbl;
    

    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    protected:
    Ui::MainWindow *ui;

    QPoint start, end;
    bool firstClick;
    

    private slots:
    void on_pushButton_clicked();
    };
    class Label : public QLabel
    {

    public:
    explicit Label(QWidget * parent);
    private:

    QPoint start, end;
    bool firstClick;
    

    protected:
    void mousePressEvent(QMouseEvent *event);

    void paintEvent(QPaintEvent *event);
    

    };

    0_1559946638629_img.png

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Because you're simply creating a QLabel with the dailog as parent but don't add it to the dialog's layout.
      Why do you want to add it manually when you already creating a ui wih the designer where you also can but the label in?

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      1

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved