PDA

View Full Version : Help with the basics setting up a Qt Gui



charliec2uk
13th August 2013, 15:51
Hi all!

I'm really just getting started with Qt and learning the basics. Historically I've only ever really done Command Line driven stuff, with minimal event drivers.

I wrote a Sudoku Solver in C++ (big whoop I know, but we all started somewhere!). I'd trying to put a Gui together, but I'm really struggling with the basics here. I'd like to set up a 9x9 grid where such that I can display the results of my solver. I thought of using QTableWidget for this, but that was a bit a of a disaster. I thought about doing just a simple matrix of 81 text boxes but can't understand how the gridlayout widget works (is it just me or does a lot of the Qt documentation leave a lot of to be desired - a few examples surely wouldn't kill them). Also - with the 81 text boxes approach, it seems so in-elegant.

Anyway, I thought I'd appeal to the good nature of the coding community and say that I'd really appreciate some handholding.

Thank you in anticipation of your help!

Santosh Reddy
13th August 2013, 17:08
I hope you will find it useful


#include <QtCore>
#include <QtWidgets>

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

QWidget mainWidget;
mainWidget.setWindowTitle("3x3x3x3");

QGridLayout * mainLayout = new QGridLayout(&mainWidget);
mainLayout->setSpacing(0);

for(int mr = 0; mr < 3; mr++)
{
for(int mc = 0; mc < 3; mc++)
{
QFrame * widget = new QFrame;
widget->setFrameStyle(QFrame::Plain);
widget->setFrameShape(QFrame::Box);

QGridLayout * gridLayout = new QGridLayout(widget);
gridLayout->setSpacing(0);
gridLayout->setMargin(0);

for(int r = 0; r < 3; r++)
{
for(int c = 0; c < 3; c++)
{
QLabel * tile = new QLabel("X");
tile->setFrameStyle(QFrame::Plain);
tile->setFrameShape(QFrame::Box);
tile->setMargin(5);

gridLayout->addWidget(tile, r, c, 1, 1, Qt::AlignCenter);
}
}

mainLayout->addWidget(widget, mr, mc, 1, 1, Qt::AlignCenter);
}
}

mainWidget.show();

return app.exec();
}

#include "main.moc"


9414