You are not logged in.

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

1

Thursday, April 9th 2015, 7:05pm

New programmer!!!

So l want to make a monopoly game (or something like that) and l'm still stuck at the beggining (l started like 2 hours ago) but my problem is that l can't connect push buttons to line edits. Here's my code:


main.cpp
#include "mainwindow.h"

#include <QApplication>

#include <QPushButton>

#include <QLineEdit>

int main(int argc, char *argv[]){

QApplication a(argc, argv);

MainWindow w;

QPushButton *rollDice = new QPushButton;

QLineEdit *rollLine = new QLineEdit;

QObject::connect(rollDice, SIGNAL(clicked()), rollLine, SLOT(clear()));

w.show();

return a.exec();

}


mainwindow.cpp
#include "mainwindow.h"

#include "ui_mainwindow.h"

#include <ctime>

#include <cstdlib>

#include <QPushButton>

#include <QLineEdit>

MainWindow::MainWindow(QWidget *parent) :QMainWindow(parent),ui(new Ui::MainWindow){

ui->setupUi(this);

QString balancePlayer1="200";

QString balancePlayer2="200";

QString housesPlayer1="0";

QString housesPlayer2="0";

QString hotelsPlayer1="0";

QString hotelsPlayer2="0";

QString jailCardsPlayer1="0";

QString jailCardsPlayer2="0";

QPushButton *rollDice = new QPushButton;

ui->lineEdit->setText(balancePlayer1);

ui->lineEdit_2->setText(balancePlayer2);

ui->lineEdit_3->setText(housesPlayer1);

ui->lineEdit_4->setText(housesPlayer2);

ui->lineEdit_5->setText(hotelsPlayer1);

ui->lineEdit_6->setText(hotelsPlayer2);

ui->lineEdit_7->setText(jailCardsPlayer1);

ui->rollLine->setText(jailCardsPlayer2);

}


MainWindow::~MainWindow()

{

delete ui;

}

void MainWindow::on_rollDice_clicked()

{

srand(time(NULL));

int randnumber = rand() %6 + 1;

QString inttostr;

inttostr.append(QString("%1").arg(randnumber));

ui->rollLine->setText(inttostr);

}

2

Thursday, April 9th 2015, 7:10pm

So that uou know, l want to select a random number (when the button is clicked) and then print it to te line edit (rollLine)

Similar threads

Used tags

c++ qt ui