[Tex/LaTex] How to set frame size in a listing

framedline-breakinglistings

I'm trying to put a frame around my code, but the frame has the wrong dimension. How can I set them right?

\begin{lstlisting}[language=C++,frame=single,tabsize=2,showspaces=false,showstringspaces=false,
  keywordstyle=\color{blue},morekeywords={QStringList,QDate,QString,QIODevice},commentstyle=\color{CadetBlue},caption={Zistenie, či sme v daný deň, už záznam o rýchlosti uložili}]
QStringList lines;
//ziskaj aktualny datum
QDate date = QDate::currentDate(); 
//preved ho na retazec
QString dateString = date.toString("dd.MM."); 
if (speed_file.open(QIODevice::ReadOnly)) {
  if (speed_file.size() != 0) {
    while (!out.atEnd()) {
    //nacitaj cely subor po riadkoch
      lines.append(out.readLine()); 
    }
    out.flush();
    speed_file.close();
    //uloz posledny riadok
    lastLine = lines[lines.size() - 1]; 
    if (dateString.compare(lastLine.mid(lastLine.indexOf("->") + 2, -1)) == 0) {
      //prepíšeme pôvodný záznam o rýchlosti
    }
  \end{lstlisting}

and this is the result

enter image description here

Best Answer

Use breaklines=true:

\documentclass{article}
\usepackage[svgnames]{xcolor}
\usepackage{listings}
\begin{document}
\begin{lstlisting}[
    language=C++,
    frame=single,
    tabsize=2,
    showspaces=false,
    showstringspaces=false,
    keywordstyle=\color{blue},
    morekeywords={QStringList,QDate,QString,QIODevice},
    commentstyle=\color{CadetBlue},
    caption={Zistenie, či sme v daný deň, už záznam o rýchlosti uložili},
    breaklines=true
]
QStringList lines;
//ziskaj aktualny datum
QDate date = QDate::currentDate(); 
//preved ho na retazec
QString dateString = date.toString("dd.MM."); 
if (speed_file.open(QIODevice::ReadOnly)) {
  if (speed_file.size() != 0) {
    while (!out.atEnd()) {
    //nacitaj cely subor po riadkoch
      lines.append(out.readLine()); 
    }
    out.flush();
    speed_file.close();
    //uloz posledny riadok
    lastLine = lines[lines.size() - 1]; 
    if (dateString.compare(lastLine.mid(lastLine.indexOf("->") + 2, -1)) == 0) {
      //prepíšeme pôvodný záznam o rýchlosti
    }
  \end{lstlisting}
\end{document}

enter image description here