QTableView is an interesting concept in QT. I was exploring it today and decided to post what I learnt from that.. I hope you will find it interesting/useful.

Download Source –>Download QTableView Source - Downloaded 4410 times

Create a new project and lets get started.. My folder structure is as below..

How my QT Creator Folder Strucutre Looks...

Open the forms->mainwindow.ui and insert a TableView into the form as below,

Insert TableView


Continue reading

Share this:
Share this page via Email Share this page via Stumble Upon Share this page via Digg this Share this page via Facebook Share this page via Twitter

 

In due course of time, once you start getting comfortable with c++ you will get ambitious and want to build a multiple window application as in visual studio or other languages. It happened to me too.. The major problem I faced (which many may find silly), was how to show the child window from parent window on button click and how to show the parent window when the child window is closed.

This small tut is the result of that confusion and search.

The below diagram is what I am trying to accomplish, (The “Close Button” on the child window means the “X” button used to close the window)

What I am trying to accomplish


Continue reading

Share this:
Share this page via Email Share this page via Stumble Upon Share this page via Digg this Share this page via Facebook Share this page via Twitter

 

I recently started working with c++ and it was a need to create a simple gui application. Most people who come from Visual Studio backgrounds know how easy it is to create a GUI application in seconds using visual studio..

Well, I found out, it was equally easy in C++ (The Myth). Well, after I started I hit on a problem. As usual, I wanted to display my window in the center on the screen when starting. In visual studio, all I had to was select the form properties and set the position.

C++ does not have that easy way with QT. I scoured the net to find some examples and I am sharing here the one I thought the best and efficient.

Open your filename.cpp inside QT Creator or your favorite IDE of choice,

Your main CPP file might like look like this at the first point. This code will create the GUI window in the top left corner.

YourClassName::YourClassName(QWidget *parent):QFrame(parent),ui(new Ui::YourClassName)
{
  ui->setupUi(this);
}

Now to center the window, Copy paste the below code and try executing it,

YourClassName::YourClassName(QWidget *parent):QFrame(parent),ui(new Ui::YourClassName)
{
  ui->setupUi(this);
  QRect position = frameGeometry();
  position.moveCenter(QDesktopWidget().availableGeometry().center());
  move(position.topLeft());
}

You have to import,

#include “QRect”
#include “QDesktopWidget”
 

If you don’t import QDesktopWidget, you will get a myriad of errors saying improper struct etc.. After the above code is inserted into your constructor, the window will appear in the center of the screen.

Leave your comments if you found a better way or if you run into a wall…

Share this:
Share this page via Email Share this page via Stumble Upon Share this page via Digg this Share this page via Facebook Share this page via Twitter